﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbouiCOM;
using System.Collections;
using SAPbobsCOM;
using BoAddonBiz_FreightForwarder.UpLoad;
using System.Xml;
using System.Text.RegularExpressions;

namespace BoAddonBiz_FreightForwarder.BusinessBase
{
    public abstract class B1Form_Quote_Base : SAPBoAddon.B1Form
    {
        #region abstract variable
        protected abstract string AVA_OQOT { get; }
        protected abstract string AVA_QOT1 { get; }
        protected abstract string AVA_QOT2 { get; }
        protected abstract string AVA_QOT3 { get; }
        protected abstract string AVA_QOT4 { get; }
        protected abstract string AVA_QOT5 { get; }
        protected abstract string AVA_QOT6 { get; }
        protected abstract string AVA_QOT7 { get; }
        protected abstract string Target_Delegate_Object { get; }
        protected abstract string Target_SalesOrder_Object { get; }
        //protected virtual Dictionary<string, string> Configs
        //{
        //    get
        //    {
        //        /* 懒惰初始化方案
        //         * 采用的原则是——
        //         * 如果UserDataSource有，优先从UserDataSource里读取
        //         * 如果UserDataSource里没有，从资源文件里读取，并写入UserDataSource
        //         * 保证能够成功读取
        //         **/

        //        Dictionary<string, string> _Configs = null;
        //        _Configs = Utils.InitialConfig(SBOApp, this);

        //        return _Configs;
        //    }
        //}
        #endregion

        #region globlal variable
        protected SAPbouiCOM.BoFormMode CurrentMode;
        protected int CargoCurrentOffset = 0;//货物信息的当前行   
        protected int FileCurrentOffset = 0;//附件信息的当前行       
        protected string CurrentMatrix = string.Empty;//当前Matrix
        ///// <summary>
        ///// Matrix和描述的对应关系
        ///// </summary>
        //private string[] MatrixNames
        //{
        //    get
        //    {
        //        if (Configs.ContainsKey("MTX_NAMES"))
        //        {
        //            return Configs["MTX_NAMES"].Split(';');
        //        }
        //        return null;
        //    }
        //}
        ///// <summary>
        ///// 允许界面进行操作的状态
        ///// </summary>
        //private BoFormMode[] EnableModes
        //{
        //    get
        //    {
        //        if (Configs.ContainsKey("ENABLE_MODES"))
        //        {
        //            List<BoFormMode> list = new List<BoFormMode>();
        //            foreach (var item in Configs["ENABLE_MODES"].Split(';'))
        //            {
        //                list.Add((BoFormMode)Enum.Parse(typeof(BoFormMode), item, true));
        //            }
        //            return list.ToArray();
        //        }
        //        return null;
        //    }
        //}
        private bool AllowAddMatrixRow
        {
            get
            {
                if (CurrentForm.Mode == BoFormMode.fm_FIND_MODE || CurrentForm.Mode == BoFormMode.fm_VIEW_MODE || CurrentForm.Mode == BoFormMode.fm_PRINT_MODE)
                {
                    SBOApp.StatusBar.SetText(String.Format("当前编辑模式下不允许添加或删除行"), BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Warning);
                    return false;
                }
                if (this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT).GetValue("U_Status", 0) != "P")
                {
                    SBOApp.StatusBar.SetText(String.Format("当前单据状态下不允许添加或删除行"), BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Warning);
                    return false;
                }
                return true;
            }
        }
        ///// <summary>
        ///// 警告颜色
        ///// </summary>
        //private int WarningColor
        //{
        //    get
        //    {
        //        if (Configs.ContainsKey("WARNING_COLOR"))
        //        {
        //            return Convert.ToInt32(Configs["WARNING_COLOR"]);
        //        }
        //        return -1;
        //    }

        //}
        ///// <summary>
        ///// 常规颜色
        ///// </summary>
        //private int RegularColor
        //{
        //    get { return -1; }
        //}
        ///// <summary>
        ///// 需要对主表信息进行非空验证的字段（目前只支持非空验证）
        ///// </summary>
        //private string[] ValidFormFields
        //{
        //    get
        //    {
        //        if (Configs.ContainsKey("VALID_FORM_FIELDS"))
        //        {
        //            return Configs["VALID_FORM_FIELDS"].Split(';');
        //        }
        //        return null;
        //    }
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        //private Dictionary<string, string> FieldsUIDMapping
        //{
        //    get
        //    {
        //        return Utils.GetFieldUIDMapping(this.CurrentForm, ValidFormFields);
        //    }
        //}
        ///// <summary>
        ///// mtx1中要验证非空的列
        ///// </summary>
        //private string[] ValidMatrix1Fields
        //{
        //    get
        //    {
        //        if (Configs.ContainsKey("VALID_MTX1_FIELDS"))
        //        {
        //            return Configs["VALID_MTX1_FIELDS"].Split(';');
        //        }
        //        return null;
        //    }
        //}
        ///// <summary>
        ///// mtx2中要验证非空的列
        ///// </summary>
        //private string[] ValidMatrix2Fields
        //{
        //    get
        //    {
        //        if (Configs.ContainsKey("VALID_MTX2_FIELDS"))
        //        {
        //            return Configs["VALID_MTX2_FIELDS"].Split(';');
        //        }
        //        return null;
        //    }
        //}
        ///// <summary>
        ///// mtx4中要验证非空的列
        ///// </summary>
        //private string[] ValidMatrix4Fields
        //{
        //    get
        //    {
        //        if (Configs.ContainsKey("VALID_MTX4_FIELDS"))
        //        {
        //            return Configs["VALID_MTX4_FIELDS"].Split(';');
        //        }
        //        return null;
        //    }
        //}
        ///// <summary>
        ///// mtx5中要验证非空的列
        ///// </summary>
        //private string[] ValidMatrix5Fields
        //{
        //    get
        //    {
        //        if (Configs.ContainsKey("VALID_MTX5_FIELDS"))
        //        {
        //            return Configs["VALID_MTX5_FIELDS"].Split(';');
        //        }
        //        return null;
        //    }
        //}
        ///// <summary>
        ///// mtx6中要验证非空的列
        ///// </summary>
        //private string[] ValidMatrix6Fields
        //{
        //    get
        //    {
        //        if (Configs.ContainsKey("VALID_MTX6_FIELDS"))
        //        {
        //            return Configs["VALID_MTX6_FIELDS"].Split(';');
        //        }
        //        return null;
        //    }
        //}
        //private string[] Matrix1_DefaultValue
        //{
        //    get
        //    {
        //        if (Configs.ContainsKey("MTX1_DEFAULTVALUE"))
        //        {
        //            return Configs["MTX1_DEFAULTVALUE"].Length == 0 ? null : Configs["MTX1_DEFAULTVALUE"].Split(';');
        //        }
        //        return null;
        //    }
        //}
        //private string[] Matrix2_DefaultValue
        //{
        //    get
        //    {
        //        if (Configs.ContainsKey("MTX2_DEFAULTVALUE"))
        //        {
        //            return Configs["MTX2_DEFAULTVALUE"].Length == 0 ? null : Configs["MTX2_DEFAULTVALUE"].Split(';');
        //        }
        //        return null;
        //    }
        //}
        //private string[] Matrix4_DefaultValue
        //{
        //    get
        //    {
        //        if (Configs.ContainsKey("MTX4_DEFAULTVALUE"))
        //        {
        //            return Configs["MTX4_DEFAULTVALUE"].Length == 0 ? null : Configs["MTX4_DEFAULTVALUE"].Split(';');
        //        }
        //        return null;
        //    }
        //}
        //private string[] Matrix5_DefaultValue
        //{
        //    get
        //    {
        //        if (Configs.ContainsKey("MTX5_DEFAULTVALUE"))
        //        {
        //            return Configs["MTX5_DEFAULTVALUE"].Length == 0 ? null : Configs["MTX5_DEFAULTVALUE"].Split(';');
        //        }
        //        return null;
        //    }
        //}
        //private string[] Matrix6_DefaultValue
        //{
        //    get
        //    {
        //        if (Configs.ContainsKey("MTX6_DEFAULTVALUE"))
        //        {
        //            return Configs["MTX6_DEFAULTVALUE"].Length == 0 ? null : Configs["MTX6_DEFAULTVALUE"].Split(';');
        //        }
        //        return null;
        //    }
        //}
        #region 物料信息
        private Dictionary<string, string> _Cargo_ComboBoxUID_Field;
        /// <summary>
        /// 货物信息中ComboBox控件
        /// </summary>
        public Dictionary<string, string> Cargo_ComboBoxUID_Field
        {
            get
            {
                if (_Cargo_ComboBoxUID_Field == null)
                {
                    _Cargo_ComboBoxUID_Field = new Dictionary<string, string>();
                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(this.CurrentForm.GetAsXML());
                    XmlNodeList nodes = xml.ChildNodes[1].SelectNodes(String.Format("//item[@type='113']/specific/databind[@table='{0}']", AVA_QOT3));
                    foreach (XmlNode node in nodes)
                    {
                        string uid = node.ParentNode.ParentNode.Attributes["uid"].Value;
                        string alias = node.Attributes["alias"].Value;
                        _Cargo_ComboBoxUID_Field.Add(uid, alias);
                    }
                }
                return _Cargo_ComboBoxUID_Field;
            }
        }
        #endregion
        #endregion

        #region constructor
        public B1Form_Quote_Base()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_Quote_Base_frm_ItemEvent);
            this.frm_MenuEvent += new SAPBoAddon.IB1FormEvents.B1MenuEventEventHandler(B1Form_Quote_Base_frm_MenuEvent);
            this.frm_ChooseFormListEvent += new SAPBoAddon.IB1FormEvents.B1ChooseFormListEventEventHandler(B1Form_Quote_Base_frm_ChooseFormListEvent);
            this.frm_FormDataEvent += new SAPBoAddon.IB1FormEvents.B1FormDataEventEventHandler(B1Form_Quote_Base_frm_FormDataEvent);
        }
        #endregion

        #region events
        void B1Form_Quote_Base_frm_FormDataEvent(SAPBoAddon.B1AddonBase.B1BusinessObjectInfo pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {
                if (pVal.EventType == BoEventTypes.et_FORM_DATA_LOAD)
                {
                    SAPBoAddon.B1Assistant.Item.SetEnabled(this.CurrentForm, new string[] { "3", "7", "154", "35" }, false);
                }
            }
            else
            {
                #region /*上传附件*/
                if (pVal.EventType == BoEventTypes.et_FORM_DATA_ADD || pVal.EventType == BoEventTypes.et_FORM_DATA_UPDATE)
                {
                    try
                    {
                        Common_UpLoad oUpload = new Common_UpLoad();
                        oUpload.Uploading(this.CurrentForm, AVA_QOT7);
                        oUpload.DownButtonVisable(CurrentForm, AVA_QOT7, FileCurrentOffset, false);
                    }
                    catch (Exception ex)
                    {
                        SBOApp.SetStatusBarMessage("更新失败！错误信息：" + ex.Message, BoMessageTime.bmt_Short, false);
                        BubbleEvent = false;
                        CurrentForm.Mode = BoFormMode.fm_UPDATE_MODE;
                    }
                }
                #endregion
                if (pVal.EventType == BoEventTypes.et_FORM_DATA_LOAD && pVal.ActionSuccess)
                {
                    #region 显示行号
                    Utils.ShowDBDSNum(CurrentForm, AVA_QOT3, 0);/*货物显示行号、总行数*/
                    Utils.ShowDBDSNum(CurrentForm, AVA_QOT7, 0, "FileLine", "FileTal"); /*附件显示行号、总行数*/
                    #endregion
                    #region 设置状态控件是否可编辑  由BeforAction里移动来的  by WangPeng 2012年8月29日 10:25:32 Vincent.yin
                    //if ((this.FormSetting.FormModeSetting.DisableStatus as string[]).Contains<string>(this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT).GetValue("U_Status", 0).Trim()))//如果不可编辑
                    //{
                    //}
                    //else
                    //{
                    //    SAPBoAddon.B1Assistant.Item.SetEnabled(this.CurrentForm, new string[] { "7" }, true);
                    //}
                    #endregion
                }
            }
        }

        void B1Form_Quote_Base_frm_ChooseFormListEvent(SAPBoAddon.B1AddonBase.B1ChooseFormList pVal, ref bool BubbleEvent)
        {
            SAPbouiCOM.DBDataSource dbds = default(DBDataSource);
            Matrix mtx = default(Matrix);
            if (pVal.BeforeAction)
            {
                #region ChooseFormList显示的条件
                SAPbouiCOM.ChooseFromList cfl = this.CurrentForm.ChooseFromLists.Item(pVal.ChooseFromListUID);

                SAPbouiCOM.Conditions cons;
                SAPbouiCOM.Condition con;
                switch (pVal.ChooseFromListUID)
                {
                    case "cfl_OCRD_5":
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "CardType";
                        con.CondVal = "S";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "cfl_OCRD"://委托单位
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup13";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "cfl_OCRD_name"://谷总要求去掉此控制，2013-03-06
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.BracketOpenNum = 1;
                        con.Alias = "QryGroup16";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "CardType";
                        con.CondVal = "L";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.BracketCloseNum = 1;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "CardType";
                        con.CondVal = "C";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "cfl_OFEE_4"://费用方案
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "U_Status";
                        con.CondVal = "U";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                }

                if (pVal.ChooseFromListUID == "cfl_OFEE_4")//费用方案
                {
                    dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT4);
                    mtx = (SAPbouiCOM.Matrix)this.CurrentForm.Items.Item(pVal.ItemUID).Specific;
                    dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, pVal.Row, "col_f", dbds);
                    string LastFeeDocName = dbds.GetValue("U_FeeScenarioName", dbds.Offset).Trim();

                    string FeeDocName = SAPBoAddon.B1Assistant.Matrix.GetCellValue(mtx, "col_2", mtx.GetCellFocus().rowIndex).ToString().Trim();

                    if (!String.IsNullOrEmpty(LastFeeDocName) && LastFeeDocName == FeeDocName)
                    {
                        BubbleEvent = false;
                    }
                }
                #endregion
            }
            else
            {
                //选择用户带出业务员
                if (pVal.ChooseFromListUID == "cfl_OCRD_name" || pVal.ChooseFromListUID == "cfl_OCRD")
                {
                    //业务员
                    dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT);
                    Utils.GetSalesPerson(dbds, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                    //string sale = pVal.SelectedObjects.GetValue("U_Sale", 0).ToString();
                    //if (!String.IsNullOrEmpty(sale))
                    //{
                    //    dbdsSlpCode.SetValue("U_SlpCode", dbdsSlpCode.Offset, sale);
                    //    Recordset rs = this.SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
                    //    rs.DoQuery(String.Format("select empID from OHEM where jobTitle = '{0}'", sale));
                    //    if (rs.RecordCount == 1)
                    //    {
                    //        string temp = rs.Fields.Item("empID").Value.ToString();
                    //        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT);
                    //        dbds.SetValue("U_BnsManCode", dbds.Offset, temp);
                    //    }
                    //}

                }
                //选择业务员
                if (pVal.ChooseFromListUID == "CFL_SlpCode")
                {
                    pVal.AutoFillValue = false;
                    dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT);
                    dbds.SetValue("U_BusinessMan", dbds.Offset, pVal.SelectedObjects.GetValue("SlpName", 0).ToString());
                    dbds.SetValue("U_BnsManCode", dbds.Offset, pVal.SelectedObjects.GetValue("SlpCode", 0).ToString());
                    if (CurrentForm.Mode == BoFormMode.fm_OK_MODE || CurrentForm.Mode == BoFormMode.fm_VIEW_MODE)
                    {
                        CurrentForm.Mode = BoFormMode.fm_UPDATE_MODE;
                    }
                }
                if (pVal.ChooseFromListUID == "CFL_Operator")
                {
                    pVal.AutoFillValue = false;
                    dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT);
                    string Operator = pVal.SelectedObjects.GetValue("jobTitle", 0).ToString();
                    if (string.IsNullOrEmpty(Operator))
                    {
                        Operator = pVal.SelectedObjects.GetValue("firstName", 0).ToString().Trim() + pVal.SelectedObjects.GetValue("lastName", 0).ToString().Trim();
                        SBOApp.SetStatusBarMessage("英文名称不存在！", BoMessageTime.bmt_Short, false);
                    }
                    dbds.SetValue("U_Operator", dbds.Offset, Operator);
                    if (CurrentForm.Mode == BoFormMode.fm_OK_MODE || CurrentForm.Mode == BoFormMode.fm_VIEW_MODE)
                    {
                        CurrentForm.Mode = BoFormMode.fm_UPDATE_MODE;
                    }
                }
                if (pVal.Action_Success)
                {
                    #region 费用项
                    if (pVal.ChooseFromListUID == "cfl_OITM_1")//费用项
                    {
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT1);
                        mtx = (SAPbouiCOM.Matrix)this.CurrentForm.Items.Item(pVal.ItemUID).Specific;
                        if (pVal.SelectedObjects.Rows.Count > 1)
                        {
                            int CurrentRow = pVal.Row;
                            SAPbouiCOM.Matrix ParentMtx = CurrentForm.Items.Item("mtx_4").Specific;
                            int ParentLineId = ParentMtx.GetNextSelectedRow();
                            mtx.GetLineData(CurrentRow);
                            string CargoType = dbds.GetValue("U_CargoType", SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, CurrentRow, "col_f", dbds)).Trim();
                            for (int i = 0; i < pVal.SelectedObjects.Rows.Count; i++)
                            {
                                dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, CurrentRow, "col_f", dbds);
                                dbds.SetValue("U_LineId", dbds.Offset, ParentLineId.ToString());
                                dbds.SetValue("U_CargoType", dbds.Offset, CargoType);
                                dbds.SetValue("U_ItemCode", dbds.Offset, pVal.SelectedObjects.GetValue("ItemCode", i).ToString());
                                dbds.SetValue("U_ItemName", dbds.Offset, pVal.SelectedObjects.GetValue("ItemName", i).ToString());
                                mtx.SetLineData(CurrentRow);
                                SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(this.CurrentForm, "mtx_1", "col_f", AVA_QOT1, CurrentRow, "Lineid", "VisOrder", true);
                                CurrentRow += 1;
                            }
                        }
                        else
                        {
                            dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, pVal.Row, "col_f", dbds);
                            mtx.GetLineData(pVal.Row);
                            string FeeCode = pVal.SelectedObjects.GetValue("ItemCode", 0).ToString();
                            dbds.SetValue("U_ItemCode", dbds.Offset, FeeCode);
                            dbds.SetValue("U_ItemName", dbds.Offset, pVal.SelectedObjects.GetValue("ItemName", 0).ToString());
                            mtx.SetLineData(pVal.Row);
                            //复制费用条件模板
                            string LineId = dbds.GetValue("LineId", dbds.Offset).Trim();
                            Utils.CopyConditionTemplate(CurrentForm, "mtx_2", AVA_QOT2, LineId, FeeCode);
                            //根据港口添加费用项
                            AddRow("mtx_4", "mtx_1", AVA_QOT1, "col_14");
                        }
                    }
                    #endregion
                    #region 底价费用项
                    else if (pVal.ChooseFromListUID == "cfl_OITM_5")//底价费用项
                    {
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT5);
                        mtx = (SAPbouiCOM.Matrix)this.CurrentForm.Items.Item(pVal.ItemUID).Specific;
                        if (pVal.SelectedObjects.Rows.Count > 1)
                        {
                            int CurrentRow = pVal.Row;
                            SAPbouiCOM.Matrix ParentMtx = CurrentForm.Items.Item("mtx_4").Specific;
                            int ParentLineId = ParentMtx.GetNextSelectedRow();
                            mtx.GetLineData(CurrentRow);
                            string CargoType = dbds.GetValue("U_CargoType", SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, CurrentRow, "col_f", dbds)).Trim();
                            for (int i = 0; i < pVal.SelectedObjects.Rows.Count; i++)
                            {
                                dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, CurrentRow, "col_f", dbds);
                                dbds.SetValue("U_LineId", dbds.Offset, ParentLineId.ToString());
                                dbds.SetValue("U_CargoType", dbds.Offset, CargoType);
                                dbds.SetValue("U_ItemCode", dbds.Offset, pVal.SelectedObjects.GetValue("ItemCode", i).ToString());
                                dbds.SetValue("U_ItemName", dbds.Offset, pVal.SelectedObjects.GetValue("ItemName", i).ToString());
                                mtx.SetLineData(CurrentRow);
                                SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(this.CurrentForm, "mtx_5", "col_f", AVA_QOT5, CurrentRow, "Lineid", "VisOrder", true);
                                CurrentRow += 1;
                            }
                        }
                        else
                        {
                            dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, pVal.Row, "col_f", dbds);
                            mtx.GetLineData(pVal.Row);
                            dbds.SetValue("U_ItemCode", dbds.Offset, pVal.SelectedObjects.GetValue("ItemCode", 0).ToString());
                            dbds.SetValue("U_ItemName", dbds.Offset, pVal.SelectedObjects.GetValue("ItemName", 0).ToString());
                            mtx.SetLineData(pVal.Row);
                            //根据港口添加底价费用项
                            AddRow("mtx_4", "mtx_5", AVA_QOT5, "col_10");
                        }
                    }
                    #endregion
                    else if (pVal.ChooseFromListUID == "cfl_OCRD" || pVal.ChooseFromListUID == "cfl_OCRD_name")
                    {
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT);
                        dbds.SetValue("U_CardCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_CardName", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                        dynamic Cntct = Utils.GetDefaultContractPerson(this.SBOCompany, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        if (Cntct != null)
                        {
                            dbds.SetValue("U_CntctCode", dbds.Offset, Cntct.FullName);
                            dbds.SetValue("U_CntctPhone", dbds.Offset, Cntct.Cellolar);
                            dbds.SetValue("U_CntctFax", dbds.Offset, Cntct.Fax);
                        }
                        //string CntctPrsn = GetDefaultContractPerson(pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        //dbds.SetValue("U_CntctCode", dbds.Offset, CntctPrsn);
                    }
                    else if (pVal.ChooseFromListUID == "cfl_OCRD_5")//底价费用项的供应商
                    {
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT5);
                        mtx = (SAPbouiCOM.Matrix)this.CurrentForm.Items.Item(pVal.ItemUID).Specific;
                        dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, pVal.Row, "col_f", dbds);
                        mtx.GetLineData(pVal.Row);
                        dbds.SetValue("U_SupplierCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_SupplierName", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                        mtx.SetLineData(pVal.Row);
                    }
                    else if (pVal.ChooseFromListUID == "cfl_PORT_s")
                    {
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT4);
                        mtx = (SAPbouiCOM.Matrix)this.CurrentForm.Items.Item(pVal.ItemUID).Specific;
                        //SAPBoAddon.B1Assistant.Matrix.UpdateDataTodbDataSource(this.CurrentForm, mtx, dbds, "col_f", "LineId");
                        dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, pVal.Row, "col_f", dbds);
                        mtx.GetLineData(pVal.Row);
                        /*港口三字码*/
                        dbds.SetValue("U_StartPortCode", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortCode", 0).ToString());
                        /*港口名称  取 港口外文名称*/
                        dbds.SetValue("U_StartPort", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortNameE", 0).ToString());
                        /*国家全称*/
                        dbds.SetValue("U_StartPortCityname", dbds.Offset, pVal.SelectedObjects.GetValue("U_CountryNameE", 0).ToString());
                        mtx.SetLineData(pVal.Row);
                        //自动添加行
                        SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(this.CurrentForm, "mtx_4", "col_f", AVA_QOT4, -1, "Lineid", "VisOrder", true, "col_11");//起运港
                    }
                    else if (pVal.ChooseFromListUID == "cfl_PORT_e")
                    {
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT4);
                        mtx = (SAPbouiCOM.Matrix)this.CurrentForm.Items.Item(pVal.ItemUID).Specific;
                        dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, pVal.Row, "col_f", dbds);
                        mtx.GetLineData(pVal.Row);
                        /*港口三字码*/
                        dbds.SetValue("U_EndPortCode", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortCode", 0).ToString());
                        /*港口名称  取 港口外文名称*/
                        dbds.SetValue("U_EndPort", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortNameE", 0).ToString());
                        /*国家全称*/
                        dbds.SetValue("U_EndPortCityname", dbds.Offset, pVal.SelectedObjects.GetValue("U_CountryNameE", 0).ToString());
                        mtx.SetLineData(pVal.Row);
                    }
                    else if (pVal.ChooseFromListUID == "cfl_PORT_t")
                    {
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT4);
                        mtx = (SAPbouiCOM.Matrix)this.CurrentForm.Items.Item(pVal.ItemUID).Specific;
                        dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, pVal.Row, "col_f", dbds);
                        mtx.GetLineData(pVal.Row);
                        /*港口三字码*/
                        dbds.SetValue("U_TranshipPortCode", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortCode", 0).ToString());
                        /*港口名称  取 港口外文名称*/
                        dbds.SetValue("U_TranshipPort", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortNameE", 0).ToString());
                        /*国家全称*/
                        dbds.SetValue("U_TransPortCityname", dbds.Offset, pVal.SelectedObjects.GetValue("U_CountryNameE", 0).ToString());
                        mtx.SetLineData(pVal.Row);
                    }
                    else if (pVal.ChooseFromListUID == "cfl_ODCM_3")
                    {
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT3);
                        mtx = (SAPbouiCOM.Matrix)this.CurrentForm.Items.Item(pVal.ItemUID).Specific;
                        dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, pVal.Row, "col_f", dbds);
                        mtx.GetLineData(pVal.Row);
                        dbds.SetValue("U_DangerPro", dbds.Offset, pVal.SelectedObjects.GetValue("U_DCName", 0).ToString());
                        mtx.SetLineData(pVal.Row);
                    }
                    else if (pVal.ChooseFromListUID == "cfl_OFEE_4")//费用方案
                    {
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT4);
                        mtx = (SAPbouiCOM.Matrix)this.CurrentForm.Items.Item(pVal.ItemUID).Specific;
                        dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, pVal.Row, "col_f", dbds);
                        //string LastFeeDocName = dbds.GetValue("U_FeeScenarioName", dbds.Offset).Trim();
                        mtx.GetLineData(pVal.Row);
                        string FeeDocEntry = pVal.SelectedObjects.GetValue("DocEntry", 0).ToString().Trim();
                        //string FeeDocName = pVal.SelectedObjects.GetValue("U_ScenarioName", 0).ToString().Trim();
                        dbds.SetValue("U_FeeScenarioCode", dbds.Offset, FeeDocEntry);
                        dbds.SetValue("U_FeeScenarioName", dbds.Offset, pVal.SelectedObjects.GetValue("U_ScenarioName", 0).ToString());
                        mtx.SetLineData(pVal.Row);
                        //复制费用方案
                        //if (LastFeeDocName != FeeDocName)
                        //{
                        string LineId = dbds.GetValue("LineId", dbds.Offset).Trim();
                        int panel = CurrentForm.PaneLevel;
                        if (panel == 4)
                        {
                            CopyFeeScenario(LineId, FeeDocEntry, "mtx_5", "mtx_6");
                        }
                        else
                        {
                            CopyFeeScenario(LineId, FeeDocEntry, "mtx_1", "mtx_2");
                        }
                        //}
                    }
                    /*费用项 航空公司信息*/

                    else if (pVal.ChooseFromListUID == "CFL_ODCM")
                    {
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT3);
                        dbds.SetValue("U_DangerPro", dbds.Offset, pVal.SelectedObjects.GetValue("U_DCName", 0).ToString());
                    }
                    else if (pVal.ChooseFromListUID == "CFL_ExpressCmp")//快递公司
                    {
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT7);
                        dbds.SetValue("U_ExpressCmp", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                    }
                }
            }
        }

        void B1Form_Quote_Base_frm_MenuEvent(SAPBoAddon.B1AddonBase.B1MenuEvent pVal, ref bool BubbleEvent)
        {
            if (CurrentForm.TypeEx == this.FormSetting.FormType)
            {
                if (pVal.BeforeAction)
                {
                    #region 复制行
                    if (pVal.MenuUID == "1294")//复制行，系统默认快捷键Ctrl+M
                    {
                        //确保是Matrix控件
                        if (pVal.ItemUID.StartsWith("mtx_"))
                        {
                            Matrix mtx = this.CurrentForm.Items.Item(pVal.ItemUID).Specific;
                            int SelectedRow = mtx.GetNextSelectedRow();
                            if (AllowAddMatrixRow)
                            {
                                CopyMatrixRow(pVal.ItemUID, SelectedRow);
                            }
                            BubbleEvent = false;
                        }
                    }
                    #endregion
                    #region 添加行
                    else if (pVal.MenuUID == "1292")//添加行
                    {
                        string ItemUID = pVal.ItemUID;
                        //默认为添加到港口里
                        if (ItemUID.StartsWith("mtx_") == false)
                            ItemUID = "mtx_4";
                        switch (ItemUID)
                        {
                            case "mtx_4"://港口添加行
                                SAPbouiCOM.Matrix oMtx = CurrentForm.Items.Item("mtx_4").Specific;
                                SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(CurrentForm, "mtx_4", "col_f", AVA_QOT4, -1, "Lineid", "VisOrder", true, "col_11");
                                //SetMatrixRowDefaultValue("mtx_4", oMtx.VisualRowCount, Matrix4_DefaultValue);
                                break;
                            case "mtx_1"://费用项添加行
                                AddRow("mtx_4", "mtx_1", AVA_QOT1, "col_14", "港口");
                                break;
                            case "mtx_2"://费用项的执行条件添加行
                                AddRow("mtx_1", "mtx_2", AVA_QOT2, "col_15", "费用项");
                                break;
                            case "mtx_5"://底价费用项添加行
                                AddRow("mtx_4", "mtx_5", AVA_QOT5, "col_10", "港口");
                                break;
                            case "mtx_6"://底价费用项的执行条件添加行
                                AddRow("mtx_5", "mtx_6", AVA_QOT6, "col_15", "底价费用项");
                                break;
                        }
                    }
                    #endregion
                    #region 删除行
                    else if (pVal.MenuUID == "1293")
                    {
                        switch (pVal.ItemUID)
                        {
                            case "mtx_4"://港口删除行
                                PortDeleteRow("mtx_4", AVA_QOT4, "mtx_1", AVA_QOT1, "mtx_2", AVA_QOT2);//删除相关联的费用项及费用项的执行条件
                                PortDeleteRow("mtx_4", AVA_QOT4, "mtx_5", AVA_QOT5, "mtx_6", AVA_QOT6);//删除相关联的底价费用项及费用项的执行条件
                                break;
                            case "mtx_1"://费用项删除行
                                ChargeDeleteRow("mtx_1", AVA_QOT1, "mtx_2", AVA_QOT2);
                                break;
                            case "mtx_2"://费用项的执行条件删除行
                                SAPBoAddon.B1Assistant.Matrix.DeleteRowAndDbdsLine(CurrentForm, AVA_QOT2, "Lineid", "mtx_2", "col_f", pVal.Row, true);
                                break;
                            case "mtx_5"://底价费用项删除行
                                ChargeDeleteRow("mtx_5", AVA_QOT5, "mtx_6", AVA_QOT6);
                                break;
                            case "mtx_6"://底价费用项的执行条件删除行
                                SAPBoAddon.B1Assistant.Matrix.DeleteRowAndDbdsLine(CurrentForm, AVA_QOT6, "Lineid", "mtx_6", "col_f", pVal.Row, true);
                                break;
                        }
                        BubbleEvent = false;
                    }
                    #endregion
                }
                else
                {
                    if (pVal.MenuUID == "1287")//复制
                    {
                        //if (this.FormSetting.FormModeSetting.itmNewModeEnable == null)
                        //{
                        //    SAPBoAddon.B1Assistant.Item.SetEnabled(this.CurrentForm, this.FormSetting.FormModeSetting.itmBrowseModeEnable, true);
                        //}
                        //else
                        //{
                        //    SAPBoAddon.B1Assistant.Item.SetEnabled(this.CurrentForm, this.FormSetting.FormModeSetting.itmNewModeEnable, true);
                        //}
                        //SAPBoAddon.B1Assistant.Item.SetEnabled(this.CurrentForm, this.FormSetting.FormModeSetting.itmNewModeDisable, false);
                        //附件不复制
                        this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT7).Clear();
                    }
                }
            }
        }

        void B1Form_Quote_Base_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {
                #region  系统事件之前
                #region BeforeAction 把DBDS的数据刷到Matrix，否则只会向数据库保存Matrix显示的部分数据
                if (pVal.ItemUID == "1" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    if (CurrentForm.Mode == BoFormMode.fm_ADD_MODE || CurrentForm.Mode == BoFormMode.fm_UPDATE_MODE)
                    {
                        SAPbouiCOM.Matrix oMtxCharge = CurrentForm.Items.Item("mtx_1").Specific;
                        oMtxCharge.LoadFromDataSourceEx();
                        SAPbouiCOM.Matrix oMtxChargeCondition = CurrentForm.Items.Item("mtx_2").Specific;
                        oMtxChargeCondition.LoadFromDataSourceEx();
                        SAPbouiCOM.Matrix oMtxBaseCharge = CurrentForm.Items.Item("mtx_5").Specific;
                        oMtxBaseCharge.LoadFromDataSourceEx();
                        SAPbouiCOM.Matrix oMtxBaseChargeCondition = CurrentForm.Items.Item("mtx_6").Specific;
                        oMtxBaseChargeCondition.LoadFromDataSourceEx();
                    }
                }
                #endregion
                if (pVal.ItemUID.StartsWith("mtx_") && pVal.EventType == BoEventTypes.et_CLICK)
                {
                    CurrentMode = CurrentForm.Mode;
                }
                //发送前验证必填项功能，必填项字段在资源文件里配置（By Webb.Wen at 2012-03-12 14:49:00）
                if (pVal.ItemUID == "btn_Send" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    //Dictionary<string, string> invalid = Utils.ValidItems(this.CurrentForm, this.CurrentForm.DataSources.DBDataSources.Item(AVA_FF_OQOT), FieldsUIDMapping, RegularColor, WarningColor);
                    //if (invalid.Count > 0)
                    //{
                    //    SBOApp.StatusBar.SetText(String.Format("“{0}”不能为空", String.Join(",", Utils.AlaisID2Descr(AVA_FF_OQOT, invalid.Values))));
                    //    BubbleEvent = false;
                    //}
                }
                #endregion
            }
            else
            {
                #region  系统事件之后
                #region switch panel
                if (pVal.ItemUID == "tab_0" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CurrentForm.PaneLevel = 1;
                }
                if (pVal.ItemUID == "tab_1" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CurrentForm.PaneLevel = 2;
                }
                if (pVal.ItemUID == "tab_2" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CurrentForm.PaneLevel = 3;
                }
                if (pVal.ItemUID == "tab_3" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CurrentForm.PaneLevel = 4;
                }
                if (pVal.ItemUID == "tab_4" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CurrentForm.PaneLevel = 5;
                    #region 是否存在附件
                    Common_UpLoad Uploading = new Common_UpLoad();
                    Uploading.DownButtonVisable(CurrentForm, AVA_QOT7, FileCurrentOffset);
                    #endregion
                }
                #endregion
                #region 选中行
                //港口
                if (pVal.ItemUID == "mtx_4" && pVal.EventType == BoEventTypes.et_CLICK)
                {
                    //清楚Mtx2、Mtx6
                    CurrentForm.Items.Item("mtx_2").Specific.Clear();
                    CurrentForm.Items.Item("mtx_6").Specific.Clear();
                    SelectedRow(pVal.ColUID, pVal.Row, "mtx_4", "mtx_1", AVA_QOT1, "col_14");
                    SelectedRow(pVal.ColUID, pVal.Row, "mtx_4", "mtx_5", AVA_QOT5, "col_10");
                    if (CurrentMode == BoFormMode.fm_OK_MODE || CurrentMode == BoFormMode.fm_VIEW_MODE)
                        CurrentForm.Mode = CurrentMode;
                }
                //费用项
                if (pVal.ItemUID == "mtx_1" && pVal.EventType == BoEventTypes.et_CLICK)
                {
                    SelectedRow(pVal.ColUID, pVal.Row, "mtx_1", "mtx_2", AVA_QOT2, "col_15");
                    //设置提/送货地址
                    GetAddress();
                    if (CurrentMode == BoFormMode.fm_OK_MODE || CurrentMode == BoFormMode.fm_VIEW_MODE)
                        CurrentForm.Mode = CurrentMode;
                }
                //费用项条件值
                if (pVal.ItemUID == "mtx_2" && pVal.ColUID == "col_15" && pVal.EventType == BoEventTypes.et_VALIDATE)
                {
                    AddRow("mtx_1", "mtx_2", AVA_QOT2, "col_15");
                }
                //底价费用项
                if (pVal.ItemUID == "mtx_5" && pVal.EventType == BoEventTypes.et_CLICK)
                {
                    SelectedRow(pVal.ColUID, pVal.Row, "mtx_5", "mtx_6", AVA_QOT6, "col_15");
                    if (CurrentMode == BoFormMode.fm_OK_MODE || CurrentMode == BoFormMode.fm_VIEW_MODE)
                        CurrentForm.Mode = CurrentMode;
                }
                //底价费用项条件值
                if (pVal.ItemUID == "mtx_6" && pVal.ColUID == "col_15" && pVal.EventType == BoEventTypes.et_VALIDATE)
                {
                    AddRow("mtx_5", "mtx_6", AVA_QOT6, "col_15");
                }
                #endregion
                #region 将提货地址和送货地址回写回当前选中的费用项行
                //if (pVal.ItemUID == "mtx_1" && pVal.ColUID == "col_18" && pVal.EventType == BoEventTypes.et_VALIDATE)
                //{
                //    SetMatrixAddress();                                        
                //}
                if (pVal.ItemUID == "76" && pVal.EventType == BoEventTypes.et_VALIDATE && pVal.ItemChanged)
                {
                    SetAddress();
                }
                #endregion
                #region 数据验证-注释
                //if (pVal.EventType == BoEventTypes.et_GOT_FOCUS || pVal.EventType == BoEventTypes.et_LOST_FOCUS)
                //{
                //    if (Configs != null)
                //    {
                //        Utils.ValidItem(this.CurrentForm, this.CurrentForm.DataSources.DBDataSources.Item(AVA_FF_OQOT), FieldsUIDMapping, pVal.ItemUID, RegularColor, WarningColor);
                //    }
                //}
                #endregion
                #region 生成下游单据
                if (pVal.ItemUID == "btn_tosod" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CopyTo(Target_SalesOrder_Object);
                }
                if (pVal.ItemUID == "btn_todlg" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CopyTo(Target_Delegate_Object);
                }
                #endregion
                #region 汇率设置
                if ((pVal.ItemUID == "mtx_2" || pVal.ItemUID == "mtx_6") && pVal.EventType == BoEventTypes.et_COMBO_SELECT)//只在添加状态下？
                {
                    if (pVal.ColUID == "col_17")
                    {
                        Utils.SetRateByCurrency(this.CurrentForm, this.SBOCompany, pVal.ItemUID, "col_17", "col_18", pVal.Row, this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT).GetValue("U_DocDate", 0));
                    }
                }
                #endregion
                #region 物料尺寸失去焦点时验证输入的是否正确
                #region Linq+Lambda的总体积计算
                if (pVal.EventType == BoEventTypes.et_COMBO_SELECT && new string[] { "cargo_32", "cargo_34" }.Contains<string>(pVal.ItemUID))
                {
                    Utils.CalculateVolumeAndQuantity(SBOApp, SBOCompany, CurrentForm, AVA_QOT3, CargoCurrentOffset);
                }
                if (pVal.ItemUID == "cargo_24" && pVal.EventType == BoEventTypes.et_VALIDATE)
                {
                    Utils.CalculateVolumeAndQuantity(SBOApp, SBOCompany, CurrentForm, AVA_QOT3, CargoCurrentOffset);
                }
                if (pVal.ItemUID == "cargo_8" && pVal.ItemChanged)
                {
                    DBDataSource dbds1 = this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT3);
                    dbds1.SetValue("U_BillingWeight", dbds1.Offset, SAPBoAddon.B1Assistant.Item.GetValue(this.CurrentForm, pVal.ItemUID).ToString());
                }
                #endregion
                #endregion
                #region  货物信息
                //上一条
                if (pVal.ItemUID == "QuPrevious" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CargoCurrentOffset = Utils.Previous(SBOApp, pVal.FormUID, AVA_QOT3);
                    Utils.ShowDBDSNum(CurrentForm, AVA_QOT3, CargoCurrentOffset);/*货物显示行号、总行数*/
                }
                //下一条
                if (pVal.ItemUID == "QuoteNext" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CargoCurrentOffset = Utils.Next(SBOApp, pVal.FormUID, AVA_QOT3);
                    Utils.ShowDBDSNum(CurrentForm, AVA_QOT3, CargoCurrentOffset);/*货物显示行号、总行数*/
                }
                //添加
                if (pVal.ItemUID == "QuoteAdd" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CargoCurrentOffset = Utils.Add(SBOApp, pVal.FormUID, AVA_QOT3);
                    //下拉列表框的默认值
                    Utils.ComboBox_Cargo_SelectDefault(this.CurrentForm, Cargo_ComboBoxUID_Field, AVA_QOT3);
                    Utils.ShowDBDSNum(CurrentForm, AVA_QOT3, CargoCurrentOffset);/*货物显示行号、总行数*/
                }
                //删除
                if (pVal.ItemUID == "QuoteDel" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CargoCurrentOffset = Utils.Delete(SBOApp, pVal.FormUID, AVA_QOT3, CargoCurrentOffset);
                    Utils.ShowDBDSNum(CurrentForm, AVA_QOT3, CargoCurrentOffset);/*货物显示行号、总行数*/
                }
                //浏览所有
                if (pVal.ItemUID == "QuoSurvey" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    Utils.Survey(SBOApp, SBOCompany, pVal.FormUID, AVA_QOT3);
                    Utils.ShowDBDSNum(CurrentForm, AVA_QOT3, CargoCurrentOffset);/*货物显示行号、总行数*/
                }
                #endregion
                #region  附件信息
                //上一条
                if (pVal.ItemUID == "FlPrevious" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    FileCurrentOffset = Utils.Previous(SBOApp, pVal.FormUID, AVA_QOT7);
                    Common_UpLoad Uploading = new Common_UpLoad();
                    Uploading.DownButtonVisable(CurrentForm, AVA_QOT7, FileCurrentOffset);
                    Utils.ShowDBDSNum(CurrentForm, AVA_QOT7, FileCurrentOffset, "FileLine", "FileTal"); /*附件显示行号、总行数*/
                }
                //下一条
                if (pVal.ItemUID == "FileNext" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    FileCurrentOffset = Utils.Next(SBOApp, pVal.FormUID, AVA_QOT7);
                    Common_UpLoad Uploading = new Common_UpLoad();
                    Uploading.DownButtonVisable(CurrentForm, AVA_QOT7, FileCurrentOffset);
                    Utils.ShowDBDSNum(CurrentForm, AVA_QOT7, FileCurrentOffset, "FileLine", "FileTal"); /*附件显示行号、总行数*/
                }
                //添加
                if (pVal.ItemUID == "FileAdd" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    FileCurrentOffset = Utils.Add(SBOApp, pVal.FormUID, AVA_QOT7);
                    Utils.ShowDBDSNum(CurrentForm, AVA_QOT7, FileCurrentOffset, "FileLine", "FileTal"); /*附件显示行号、总行数*/
                }
                //删除
                if (pVal.ItemUID == "FileDel" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    FileCurrentOffset = Utils.Delete(SBOApp, pVal.FormUID, AVA_QOT7, FileCurrentOffset);
                    Utils.ShowDBDSNum(CurrentForm, AVA_QOT7, FileCurrentOffset, "FileLine", "FileTal"); /*附件显示行号、总行数*/
                }
                //浏览所有
                if (pVal.ItemUID == "FleSurvey" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    Utils.Survey(SBOApp, SBOCompany, pVal.FormUID, AVA_QOT7);
                    Utils.ShowDBDSNum(CurrentForm, AVA_QOT7, FileCurrentOffset, "FileLine", "FileTal"); /*附件显示行号、总行数*/
                }
                //选择文件路径
                if (pVal.ItemUID == "Upload" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    Common_UpLoad upload = new Common_UpLoad();
                    upload.SetAffixInfo(CurrentForm, AVA_QOT7, FileCurrentOffset);
                }
                //下载上传的文件
                if (pVal.ItemUID == "Down" && pVal.EventType == BoEventTypes.et_CLICK)
                {
                    Common_UpLoad Uploading = new Common_UpLoad();
                    Uploading.DownFile(CurrentForm, AVA_QOT7, FileCurrentOffset);
                }
                #endregion
                #region 添加行/删除行
                if (pVal.EventType == BoEventTypes.et_CLICK)
                {
                    if (pVal.ItemUID != "" && pVal.ItemUID != "btn_Add")
                    {
                        if (CurrentForm.Items.Item(pVal.ItemUID).Type == BoFormItemTypes.it_MATRIX)
                        {
                            CurrentMatrix = pVal.ItemUID;
                            if (CurrentMatrix == "mtx_4")
                            {
                                CurrentForm.Items.Item("btn_Add").Enabled = true;
                            }
                            //费用项、底价费用项
                            if ((new string[] { "mtx_1", "mtx_5" }).Contains(CurrentMatrix))
                            {
                                SAPbouiCOM.Matrix oMtx = CurrentForm.Items.Item("mtx_4").Specific;
                                if (oMtx.GetNextSelectedRow() <= 0)
                                {
                                    CurrentForm.Items.Item("btn_Add").Enabled = false;
                                }
                                else
                                    CurrentForm.Items.Item("btn_Add").Enabled = true;
                            }
                            //执行条件
                            if ("mtx_2" == CurrentMatrix)
                            {
                                SAPbouiCOM.Matrix oMtx = CurrentForm.Items.Item("mtx_1").Specific;
                                if (oMtx.GetNextSelectedRow() > 0)
                                {
                                    oMtx = CurrentForm.Items.Item("mtx_4").Specific;
                                    if (oMtx.GetNextSelectedRow() <= 0)
                                    {
                                        CurrentForm.Items.Item("btn_Add").Enabled = false;
                                    }
                                    else
                                        CurrentForm.Items.Item("btn_Add").Enabled = true;
                                }
                            }
                            //执行条件
                            if ("mtx_6" == CurrentMatrix)
                            {
                                SAPbouiCOM.Matrix oMtx = CurrentForm.Items.Item("mtx_5").Specific;
                                if (oMtx.GetNextSelectedRow() > 0)
                                {
                                    oMtx = CurrentForm.Items.Item("mtx_4").Specific;
                                    if (oMtx.GetNextSelectedRow() <= 0)
                                    {
                                        CurrentForm.Items.Item("btn_Add").Enabled = false;
                                    }
                                    else
                                        CurrentForm.Items.Item("btn_Add").Enabled = true;
                                }
                            }
                        }
                        else
                        {
                            CurrentMatrix = string.Empty;
                            CurrentForm.Items.Item("btn_Add").Enabled = false;
                        }
                    }
                }
                if (pVal.ItemUID == "btn_Add" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    Add();
                }
                #endregion
                #endregion

                if (pVal.ItemUID == "btn_Cntct" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    SAPBoAddon.B1FormList.B1FormListSetting oFormListSetting = new SAPBoAddon.B1FormList.B1FormListSetting("FileDown", SAPBoAddon.B1FormList.FL_FillValueType.AddonEdit);
                    oFormListSetting.FormTitle = "选择联系人";

                    oFormListSetting.ListSQL = String.Format("select Name,Title,Position, Cellolar,t0.Fax from OCPR t0 inner join OCRD t1 on t0.CardCode = t1.CardCode and t1.CardCode = '{0}'", this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT).GetValue("U_CardCode", 0).Trim());

                    oFormListSetting.FormType = CurrentForm.TypeEx;
                    oFormListSetting.FormUID = CurrentForm.UniqueID;
                    oFormListSetting.ItemUID = "btn_Cntct";
                    oFormListSetting.FillSelectedField = "btn_Cntct";
                    oFormListSetting.SelectionMode = BoMatrixSelect.ms_Single;

                    oFormListSetting.FindField = "Name";

                    oFormListSetting.MtxColumns.Add();
                    oFormListSetting.MtxColumns.Enabled = false;
                    oFormListSetting.MtxColumns.Title = "联系人名称";
                    oFormListSetting.MtxColumns.Add();
                    oFormListSetting.MtxColumns.Enabled = false;
                    oFormListSetting.MtxColumns.Title = "部门";
                    oFormListSetting.MtxColumns.Add();
                    oFormListSetting.MtxColumns.Enabled = false;
                    oFormListSetting.MtxColumns.Title = "职位";
                    oFormListSetting.MtxColumns.Add();
                    oFormListSetting.MtxColumns.Enabled = false;
                    oFormListSetting.MtxColumns.Title = "移动电话";
                    oFormListSetting.MtxColumns.Add();
                    oFormListSetting.MtxColumns.Enabled = false;
                    oFormListSetting.MtxColumns.Title = "传真";
                    oFormListSetting.MtxColumns.Add();

                    SAPBoAddon.B1Addon.B1Addon.B1FormList.Form_CreateListForm(oFormListSetting);
                    SAPBoAddon.B1Addon.B1Addon.B1FormList.B1ListFormEvent += new SAPBoAddon.B1FormList.B1FormList.B1ListFormEventEventHandler(B1FormList_B1ListFormEvent);
                }
            }
        }

        void B1FormList_B1ListFormEvent(SAPBoAddon.B1FormList.B1FormListEvents pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeEvent == false)
            {
                pVal.CloseForm = false;
                this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT).SetValue("U_CntctCode", 0, pVal.SelectedDataTable.GetValue("Name", 0));
                this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT).SetValue("U_CntctPhone", 0, pVal.SelectedDataTable.GetValue("Cellolar", 0));
                this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT).SetValue("U_CntctFax", 0, pVal.SelectedDataTable.GetValue("Fax", 0));
            }
        }
        #endregion

        #region override
        protected override bool BeforeSaveCheckFormData()
        {
            return Utils.CheckBusinessPartner(SBOApp, CurrentForm, AVA_OQOT, "U_CardName");
        }

        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {
            switch (frmEditMode)
            {
                case SAPBoAddon.frmItemEditMode.fe_AddMode:
#if DEBUG
                    DateTime dt1;
                    DateTime dt2;
                    TimeSpan ts;
                    dt1 = DateTime.Now;
#endif
                    SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(CurrentForm, "mtx_4", "col_f", AVA_QOT4, -1, "Lineid", "VisOrder", true);
                    SAPbouiCOM.DBDataSource dbds = CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT);
                    /*操作员，客服相关*/
                    string Operator = Utils.GetCurrentUser().Operator; ;
                    dbds.SetValue("U_Operator", dbds.Offset, Operator);
                    Utils.SelectShowEmpId(dbds, Operator, false);
                    dbds.SetValue("U_DocDate", dbds.Offset, DateTime.Now.ToString("yyyyMMdd"));
                    dbds.SetValue("UserSign", dbds.Offset, SBOCompany.UserSignature.ToString());
                    this.ComboBox_SelectDefault();
#if DEBUG
                    dt2 = DateTime.Now;
                    ts = dt2 - dt1;
                    SBOApp.StatusBar.SetText(string.Format("加载cargo默认值完成,耗时:[{0}]", ts.TotalMilliseconds.ToString()), BoMessageTime.bmt_Medium, BoStatusBarMessageType.smt_Success);
#endif
                    SAPBoAddon.B1Assistant.Item.SetEnabled(this.CurrentForm, new string[] { "7" }, true);

                    break;
                case SAPBoAddon.frmItemEditMode.fe_BrowseMode:
                    foreach (var item in new string[] { "mtx_1", "mtx_2", "mtx_5", "mtx_6" })
                    {
                        (this.CurrentForm.Items.Item(item).Specific as Matrix).Clear();
                    }
                    break;
            }
            #region 显示行号
            Utils.ShowDBDSNum(CurrentForm, AVA_QOT3, 0);/*货物显示行号、总行数*/
            Utils.ShowDBDSNum(CurrentForm, AVA_QOT7, 0, "FileLine", "FileTal"); /*附件显示行号、总行数*/
            #endregion
        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {

            FormSetting.AutoGetObjectNextNumber = true;

            #region Matrix
            //配置添加子表的行
            FormSetting.MatrixSetting.MtxUID = "mtx_1";
            FormSetting.MatrixSetting.NotAllowNoLineSave = false;
            FormSetting.MatrixSetting.AutoAddNewLine = false;
            FormSetting.MatrixSetting.MenuAddNewLine = false;
            FormSetting.MatrixSetting.AutoLineKeyCol = "col_14";//费用编码
            FormSetting.MatrixSetting.BeforeSaveDeleteNoValueLine = true;
            FormSetting.MatrixSetting.BeforeSaveFixDbDataSourceData = true;
            FormSetting.MatrixSetting.TableKeyField = "LineId";
            FormSetting.MatrixSetting.TableVisOrder = "VisOrder";
            FormSetting.MatrixSetting.PrimaryLineCol = "col_f";
            FormSetting.MatrixSetting.TableName = AVA_QOT1;
            FormSetting.MatrixSetting.Add();

            FormSetting.MatrixSetting.MtxUID = "mtx_2";
            FormSetting.MatrixSetting.AutoAddNewLine = false;
            FormSetting.MatrixSetting.MenuAddNewLine = false;
            FormSetting.MatrixSetting.AutoLineKeyCol = "col_17";//币种
            FormSetting.MatrixSetting.BeforeSaveDeleteNoValueLine = true;
            FormSetting.MatrixSetting.BeforeSaveFixDbDataSourceData = true;
            FormSetting.MatrixSetting.TableKeyField = "LineId";
            FormSetting.MatrixSetting.TableVisOrder = "VisOrder";
            FormSetting.MatrixSetting.PrimaryLineCol = "col_f";
            FormSetting.MatrixSetting.TableName = AVA_QOT2;
            FormSetting.MatrixSetting.Add();

            FormSetting.MatrixSetting.MtxUID = "mtx_4";
            FormSetting.MatrixSetting.AutoAddNewLine = false;
            FormSetting.MatrixSetting.MenuAddNewLine = false;
            FormSetting.MatrixSetting.AutoLineKeyCol = "col_10";//起运港
            FormSetting.MatrixSetting.BeforeSaveDeleteNoValueLine = true;
            FormSetting.MatrixSetting.BeforeSaveFixDbDataSourceData = true;
            FormSetting.MatrixSetting.TableKeyField = "LineId";
            FormSetting.MatrixSetting.TableVisOrder = "VisOrder";
            FormSetting.MatrixSetting.PrimaryLineCol = "col_f";
            FormSetting.MatrixSetting.TableName = AVA_QOT4;
            FormSetting.MatrixSetting.Add();

            FormSetting.MatrixSetting.MtxUID = "mtx_5";
            FormSetting.MatrixSetting.AutoAddNewLine = false;
            FormSetting.MatrixSetting.MenuAddNewLine = false;
            FormSetting.MatrixSetting.AutoLineKeyCol = "col_10";//费用编码
            FormSetting.MatrixSetting.BeforeSaveDeleteNoValueLine = true;
            FormSetting.MatrixSetting.BeforeSaveFixDbDataSourceData = true;
            FormSetting.MatrixSetting.TableKeyField = "LineId";
            FormSetting.MatrixSetting.TableVisOrder = "VisOrder";
            FormSetting.MatrixSetting.PrimaryLineCol = "col_f";
            FormSetting.MatrixSetting.TableName = AVA_QOT5;
            FormSetting.MatrixSetting.Add();

            FormSetting.MatrixSetting.MtxUID = "mtx_6";
            FormSetting.MatrixSetting.AutoAddNewLine = false;
            FormSetting.MatrixSetting.MenuAddNewLine = false;
            FormSetting.MatrixSetting.AutoLineKeyCol = "col_17";//币种
            FormSetting.MatrixSetting.BeforeSaveDeleteNoValueLine = true;
            FormSetting.MatrixSetting.BeforeSaveFixDbDataSourceData = true;
            FormSetting.MatrixSetting.TableKeyField = "LineId";
            FormSetting.MatrixSetting.TableVisOrder = "VisOrder";
            FormSetting.MatrixSetting.PrimaryLineCol = "col_f";
            FormSetting.MatrixSetting.TableName = AVA_QOT6;
            FormSetting.MatrixSetting.Add();
            #endregion

            #region 控件是否可编辑
            string[] ItemList;
            string[] itmBrowseModeDisable;
            string[] itmBrowseModeEnable;
            string[] itmFindModeEnable;
            string[] itmNewModeDisable = new string[] { "3", "7", "154", "35" };
            string[] filter = new string[] { "btn_Send", "btn_tosod", "btn_todlg", "QuPrevious", "QuoteNext", "QuoSurvey", "FlPrevious", "FileNext", "FleSurvey" };

            FormSetting.FormModeSetting.itmNewModeDisable = itmNewModeDisable;
            FormSetting.FormModeSetting.StatusItemUID = "7";
            SAPBoAddon.BrowseModeSetting ModeSetting;

            //浏览不可编辑的状态
            ItemList = Utils.GetFormItems(FormSetting.FormXmlDocument, true, filter);
            itmBrowseModeDisable = new string[ItemList.Length]; ;
            ItemList.CopyTo(itmBrowseModeDisable, 0);

            //浏览可编辑的状态
            ItemList = Utils.GetFormItems(FormSetting.FormXmlDocument, true, itmNewModeDisable);
            itmBrowseModeEnable = new string[ItemList.Length + 1];
            ItemList.CopyTo(itmBrowseModeEnable, 0);

            ModeSetting = FormSetting.FormModeSetting.BrowseModeSettingList.Add();
            ModeSetting.Status = "P";
            //ModeSetting.itmBrowseModeDisable = itmBrowseModeDisable;
            itmBrowseModeEnable[itmBrowseModeEnable.Length - 1] = "7";
            ModeSetting.itmBrowseModeEnable = itmBrowseModeEnable;

            ModeSetting = FormSetting.FormModeSetting.BrowseModeSettingList.Add();
            ModeSetting.Status = "A";
            ModeSetting.itmBrowseModeDisable = itmBrowseModeDisable;
            //ModeSetting.itmBrowseModeEnable = itmBrowseModeEnable;

            ModeSetting = FormSetting.FormModeSetting.BrowseModeSettingList.Add();
            ModeSetting.Status = "R";
            ModeSetting.itmBrowseModeDisable = itmBrowseModeDisable;
            //ModeSetting.itmBrowseModeEnable = itmBrowseModeEnable;

            ModeSetting = FormSetting.FormModeSetting.BrowseModeSettingList.Add();
            ModeSetting.Status = "F";
            ModeSetting.itmBrowseModeDisable = itmBrowseModeDisable;
            //ModeSetting.itmBrowseModeEnable = itmBrowseModeEnable;

            ModeSetting = FormSetting.FormModeSetting.BrowseModeSettingList.Add();
            ModeSetting.Status = "D";
            ModeSetting.itmBrowseModeDisable = itmBrowseModeDisable;
            //ModeSetting.itmBrowseModeEnable = itmBrowseModeEnable;

            //查找状态
            ItemList = Utils.GetFormItems(FormSetting.FormXmlDocument);
            itmFindModeEnable = new string[ItemList.Length + itmNewModeDisable.Length];
            ItemList.CopyTo(itmFindModeEnable, 0);
            itmNewModeDisable.CopyTo(itmFindModeEnable, ItemList.Length);
            FormSetting.FormModeSetting.itmFindModeEnable = itmFindModeEnable;
            #endregion
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
#if DEBUG
            DateTime dt1 = DateTime.Now;
#endif
            this.CurrentForm.Freeze(true);
            /*加载下拉列表可选值*/
            SAPbouiCOM.ComboBox oCmb;
            oCmb = this.CurrentForm.Items.Item("321").Specific;
            if (oCmb.ValidValues.Count <= 0)
            {
                this.Combobox_Initialize();
            }
            /*清空【费用项】、【底价费用项】及对应【执行条件】表格数据  这TM什么需求啊？靠~我还以为数据丢了呢  [2011-12-21 wangpeng]*/
            foreach (var item in new string[] { "mtx_1", "mtx_2", "mtx_5", "mtx_6" })
            {
                (this.CurrentForm.Items.Item(item).Specific as Matrix).Clear();
            }
            //加载用户
            Utils.LoadUser(this.CurrentForm, this.SBOCompany, "154");
            //审批人
            Utils.LoadUser(this.CurrentForm, this.SBOCompany, "txtApp");

            if (new Regex(@"\b\d+\b").Match(InitPar.FatherFormUID).Success)
            {
                this.CurrentForm.Mode = SAPbouiCOM.BoFormMode.fm_FIND_MODE;
                SAPBoAddon.B1Assistant.Item.SetValue(this.CurrentForm, this.CurrentForm.DataBrowser.BrowseBy, InitPar.FatherFormUID);
                this.CurrentForm.Items.Item("1").Click(SAPbouiCOM.BoCellClickType.ct_Regular);
            }

            this.CurrentForm.Freeze(false);
#if DEBUG
            DateTime dt2 = DateTime.Now;
            TimeSpan ts = dt2 - dt1;
            string str_d = ts.TotalMilliseconds.ToString();
            SBOApp.StatusBar.SetText(string.Format("窗体加载完成,耗时:[{0}]", str_d), BoMessageTime.bmt_Medium, BoStatusBarMessageType.smt_Success);
#endif
        }
        #endregion

        #region private methods
        #region 生成下游单据
        /// <summary>
        /// 复制到下游单据
        /// </summary>
        /// <param name="pVal"></param>
        protected void CopyTo(string targetObject)
        {
            if (this.CurrentForm.Mode != BoFormMode.fm_OK_MODE)
            {
                SBOApp.StatusBar.SetText(String.Format("请先点击“更新”或“添加”，保存当前单据。"), SAPbouiCOM.BoMessageTime.bmt_Medium, SAPbouiCOM.BoStatusBarMessageType.smt_Warning);
                return;
            }
            if (this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT).GetValue("U_Status", 0) != "R")
            {
                SBOApp.StatusBar.SetText(String.Format("只有批准状态下的报价才能生成下游单据"), SAPbouiCOM.BoMessageTime.bmt_Medium, SAPbouiCOM.BoStatusBarMessageType.smt_Warning);
                return;
            }
            this.ShowForm(targetObject, this.CurrentForm.UniqueID);
        }
        #endregion
        #region 复制行
        /// <summary>
        /// 复制港口，联动复制费用项和费用条件、底价费用项和底价费用条件（老子懒得递归了，下同）
        /// </summary>
        /// <param name="row"></param>
        /// <param name="check"></param>
        /// <authro>Webb.Wen</authro>
        /// <create>2012-03-10 13:37:26</create>
        private void CopyMatrix4Row(int row, List<string> check)
        {
            Matrix mtx4 = this.CurrentForm.Items.Item("mtx_4").Specific;
            DBDataSource dbds4 = this.CurrentForm.DataSources.DBDataSources.Item(mtx4.Columns.Item("col_f").DataBind.TableName);
            Matrix mtx1 = this.CurrentForm.Items.Item("mtx_1").Specific;
            DBDataSource dbds1 = this.CurrentForm.DataSources.DBDataSources.Item(mtx1.Columns.Item("col_f").DataBind.TableName);
            Matrix mtx2 = this.CurrentForm.Items.Item("mtx_2").Specific;
            DBDataSource dbds2 = this.CurrentForm.DataSources.DBDataSources.Item(mtx2.Columns.Item("col_f").DataBind.TableName);
            Matrix mtx5 = this.CurrentForm.Items.Item("mtx_5").Specific;
            DBDataSource dbds5 = this.CurrentForm.DataSources.DBDataSources.Item(mtx5.Columns.Item("col_f").DataBind.TableName);
            Matrix mtx6 = this.CurrentForm.Items.Item("mtx_6").Specific;
            DBDataSource dbds6 = this.CurrentForm.DataSources.DBDataSources.Item(mtx6.Columns.Item("col_f").DataBind.TableName);
            SAPBoAddon.B1Assistant.Matrix.UpdateDataTodbDataSource(CurrentForm, mtx1, dbds1, "col_f", "LineId");
            SAPBoAddon.B1Assistant.Matrix.UpdateDataTodbDataSource(CurrentForm, mtx2, dbds2, "col_f", "LineId");
            SAPBoAddon.B1Assistant.Matrix.UpdateDataTodbDataSource(CurrentForm, mtx5, dbds5, "col_f", "LineId");
            SAPBoAddon.B1Assistant.Matrix.UpdateDataTodbDataSource(CurrentForm, mtx6, dbds6, "col_f", "LineId");

            int offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx4, row, "col_f", dbds4);
            dbds4.Offset = offset;
            mtx4.SetLineData(row);
            List<string> checkResult = Utils.CheckMatrixRow(mtx4, row, check);
            if (checkResult.Count != 0)
            {
                SBOApp.StatusBar.SetText(String.Format("第{0}行“{1}”不能为空", row, string.Join("”,“", checkResult)));
            }
            else
            {
                SBOApp.StatusBar.SetText("开始复制港口...", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Warning);
                Utils.RemoveMatrixNoValueLines(mtx4, dbds4, check, "col_f");
                Utils.CopyDbdsLine(dbds4, offset);
                mtx4.AddRow(1);
                dbds4.Offset = dbds4.Size - 1;
                mtx4.GetLineData(mtx4.VisualRowCount);
                mtx4.SelectRow(mtx4.VisualRowCount, true, false);
                SBOApp.StatusBar.SetText("港口复制完成", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Warning);
                SBOApp.StatusBar.SetText("开始复制费用项和费用条件...", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Warning);
                string mtx4LineId = dbds4.GetValue("LineId", dbds4.Size - 1).Trim();
                Dictionary<string, string> dbds1Initial = new Dictionary<string, string>();
                dbds1Initial.Add("U_LineID", mtx4LineId);

                int dbds1Size = dbds1.Size;
                for (int i = 0; i < dbds1Size; i++)
                {
                    if (dbds1.GetValue("U_LineID", i).Trim() == dbds4.GetValue("LineId", offset).Trim())
                    {
                        Utils.RemoveMatrixNoValueLines(mtx1, dbds1, new List<string> { "U_ItemName", "U_CargoType" }, "col_f");
                        Utils.CopyDbdsLine(dbds1, i, dbds1Initial);

                        string mtx1LineId = dbds1.GetValue("LineId", dbds1.Size - 1).Trim();
                        Dictionary<string, string> dbds2Initial = new Dictionary<string, string>();
                        dbds2Initial.Add("U_LineID", mtx1LineId);
                        int dbds2Size = dbds2.Size;
                        for (int ii = 0; ii < dbds2Size; ii++)
                        {
                            if (dbds2.GetValue("U_LineID", ii).Trim() == dbds1.GetValue("LineId", i).Trim())
                            {
                                Utils.RemoveMatrixNoValueLines(mtx2, dbds2, new List<string> { "U_Condition", "U_Value" }, "col_f");
                                Utils.CopyDbdsLine(dbds2, ii, dbds2Initial);
                            }
                        }
                    }
                }
                SBOApp.StatusBar.SetText("费用项和费用条件复制完成", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Warning);
                SBOApp.StatusBar.SetText("开始复制底价费用项和底价费用条件...", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Warning);
                int dbds5Size = dbds5.Size;
                for (int i = 0; i < dbds5Size; i++)
                {
                    if (dbds5.GetValue("U_LineID", i).Trim() == dbds4.GetValue("LineId", offset).Trim())
                    {
                        Utils.RemoveMatrixNoValueLines(mtx5, dbds5, new List<string> { "U_ItemName", "U_CargoType" }, "col_f");
                        Utils.CopyDbdsLine(dbds5, i, dbds1Initial);

                        string mtx1LineId = dbds5.GetValue("LineId", dbds5.Size - 1).Trim();
                        Dictionary<string, string> dbds5Initial = new Dictionary<string, string>();
                        dbds5Initial.Add("U_LineID", mtx1LineId);
                        int dbds6Size = dbds6.Size;
                        for (int ii = 0; ii < dbds6Size; ii++)
                        {
                            if (dbds6.GetValue("U_LineID", ii).Trim() == dbds5.GetValue("LineId", i).Trim())
                            {
                                Utils.RemoveMatrixNoValueLines(mtx6, dbds6, new List<string> { "U_Condition", "U_Value" }, "col_f");
                                Utils.CopyDbdsLine(dbds6, ii, dbds5Initial);
                            }
                        }
                    }
                }
                SBOApp.StatusBar.SetText("底价费用项和底价费用条件复制完成", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Warning);
                SBOApp.StatusBar.SetText("复制成功！", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Success);
            }
        }
        /// <summary>
        /// 复制费用项，联动费用条件
        /// </summary>
        /// <param name="row"></param>
        /// <param name="check"></param>
        /// <authro>Webb.Wen</authro>
        /// <create>2012-03-10 13:37:54</create>
        private void CopyMatrix1Row(int row, List<string> check)
        {
            Matrix mtx1 = this.CurrentForm.Items.Item("mtx_1").Specific;
            DBDataSource dbds1 = this.CurrentForm.DataSources.DBDataSources.Item(mtx1.Columns.Item("col_f").DataBind.TableName);
            Matrix mtx2 = this.CurrentForm.Items.Item("mtx_2").Specific;
            DBDataSource dbds2 = this.CurrentForm.DataSources.DBDataSources.Item(mtx2.Columns.Item("col_f").DataBind.TableName);
            SAPBoAddon.B1Assistant.Matrix.UpdateDataTodbDataSource(CurrentForm, mtx2, dbds2, "col_f", "LineId");

            int offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx1, row, "col_f", dbds1);
            dbds1.Offset = offset;
            mtx1.SetLineData(row);
            List<string> checkResult = Utils.CheckMatrixRow(mtx1, row, check);
            if (checkResult.Count != 0)
            {
                SBOApp.StatusBar.SetText(String.Format("第{0}行“{1}”不能为空", row, string.Join("”,“", checkResult)));
            }
            else
            {
                Utils.RemoveMatrixNoValueLines(mtx1, dbds1, check, "col_f");
                Utils.CopyDbdsLine(dbds1, offset);
                mtx1.AddRow(1);
                dbds1.Offset = dbds1.Size - 1;
                mtx1.GetLineData(mtx1.VisualRowCount);
                mtx1.SelectRow(mtx1.VisualRowCount, true, false);

                string mtx1LineId = dbds1.GetValue("LineId", dbds1.Size - 1).Trim();
                Dictionary<string, string> dbds1Initial = new Dictionary<string, string>();
                dbds1Initial.Add("U_LineID", mtx1LineId);

                int dbds2Size = dbds2.Size;
                for (int i = 0; i < dbds2Size; i++)
                {
                    if (dbds2.GetValue("U_LineID", i).Trim() == dbds1.GetValue("LineId", offset).Trim())
                    {
                        Utils.RemoveMatrixNoValueLines(mtx2, dbds2, new List<string> { "U_Condition", "U_Value" }, "col_f");
                        Utils.CopyDbdsLine(dbds2, i, dbds1Initial);
                    }
                }
            }
        }
        /// <summary>
        /// 复制费用项，联动费用条件
        /// </summary>
        /// <param name="row"></param>
        /// <param name="check"></param>
        /// <authro>Webb.Wen</authro>
        /// <create>2012-03-10 13:37:54</create>
        private void CopyMatrix5Row(int row, List<string> check)
        {
            Matrix mtx5 = this.CurrentForm.Items.Item("mtx_5").Specific;
            DBDataSource dbds5 = this.CurrentForm.DataSources.DBDataSources.Item(mtx5.Columns.Item("col_f").DataBind.TableName);
            Matrix mtx6 = this.CurrentForm.Items.Item("mtx_6").Specific;
            DBDataSource dbds6 = this.CurrentForm.DataSources.DBDataSources.Item(mtx6.Columns.Item("col_f").DataBind.TableName);
            SAPBoAddon.B1Assistant.Matrix.UpdateDataTodbDataSource(CurrentForm, mtx6, dbds6, "col_f", "LineId");

            int offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx5, row, "col_f", dbds5);
            dbds5.Offset = offset;
            mtx5.SetLineData(row);
            List<string> checkResult = Utils.CheckMatrixRow(mtx5, row, check);
            if (checkResult.Count != 0)
            {
                SBOApp.StatusBar.SetText(String.Format("第{0}行“{1}”不能为空", row, string.Join("”,“", checkResult)));
            }
            else
            {
                Utils.RemoveMatrixNoValueLines(mtx5, dbds5, check, "col_f");
                Utils.CopyDbdsLine(dbds5, offset);
                mtx5.AddRow(1);
                dbds5.Offset = dbds5.Size - 1;
                mtx5.GetLineData(mtx5.VisualRowCount);
                mtx5.SelectRow(mtx5.VisualRowCount, true, false);

                string mtx1LineId = dbds5.GetValue("LineId", dbds5.Size - 1).Trim();
                Dictionary<string, string> dbds1Initial = new Dictionary<string, string>();
                dbds1Initial.Add("U_LineID", mtx1LineId);

                int dbds2Size = dbds6.Size;
                for (int i = 0; i < dbds2Size; i++)
                {
                    if (dbds6.GetValue("U_LineID", i).Trim() == dbds5.GetValue("LineId", offset).Trim())
                    {
                        Utils.RemoveMatrixNoValueLines(mtx6, dbds6, new List<string> { "U_Condition", "U_Value" }, "col_f");
                        Utils.CopyDbdsLine(dbds6, i, dbds1Initial);
                    }
                }
            }
        }
        /// <summary>
        /// 没有联动的常规复制，自动复制U_LineID
        /// </summary>
        /// <param name="mtx"></param>
        /// <param name="row"></param>
        /// <param name="check"></param>
        /// <authro>Webb.Wen</authro>
        /// <create>2012-03-10 13:50:00</create>
        protected void CopyMatrixRow(string mtx_uid, int row, List<string> check)
        {
            Matrix mtx = this.CurrentForm.Items.Item(mtx_uid).Specific;
            DBDataSource dbds = this.CurrentForm.DataSources.DBDataSources.Item(mtx.Columns.Item("col_f").DataBind.TableName);
            int offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, row, "col_f", dbds);
            dbds.Offset = offset;
            mtx.SetLineData(row);
            List<string> checkResult = Utils.CheckMatrixRow(mtx, row, check);
            if (checkResult.Count != 0)
            {
                SBOApp.StatusBar.SetText(String.Format("第{0}行“{1}”不能为空", row, string.Join("”,“", checkResult)));
            }
            else
            {
                Utils.RemoveMatrixNoValueLines(mtx, dbds, check, "col_f");
                Dictionary<string, string> initial = new Dictionary<string, string>();
                List<string> fields = Utils.GetDbdsFields(dbds);
                if (fields.Contains("U_LineId"))
                {
                    initial.Add("U_LineId", dbds.GetValue("U_LineId", offset));
                }
                Utils.CopyDbdsLine(dbds, offset, initial.Count == 0 ? null : initial);
                mtx.AddRow(1);
                dbds.Offset = dbds.Size - 1;
                mtx.GetLineData(mtx.VisualRowCount);
                mtx.SelectRow(mtx.VisualRowCount, true, false);
            }
        }
        /// <summary>
        /// 复制Matrix行
        /// </summary>
        /// <param name="mtx_uid"></param>
        /// <param name="key_column"></param>
        /// <param name="matrix_row"></param>
        protected void CopyMatrixRow(string mtx_uid, string TableName, string key_column, int matrix_row)
        {
            Matrix mtx = this.CurrentForm.Items.Item(mtx_uid).Specific;
            DBDataSource dbds = this.CurrentForm.DataSources.DBDataSources.Item(TableName);

            IEnumerator enumerator = mtx.Columns.GetEnumerator();
            Dictionary<string, string> columns = new Dictionary<string, string>();
            while (enumerator.MoveNext())
            {
                Column column = enumerator.Current as Column;
                columns.Add(column.UniqueID, column.Title);
            }
            if (mtx.VisualRowCount > 0)//如果当前Matrix有行，则取最后一行主键，如果非空，则新加一行
            {
                SAPBoAddon.B1Assistant.Matrix.UpdateDataTodbDataSource(this.CurrentForm, mtx, dbds, "col_f", "LineId");

                string currentKey = SAPBoAddon.B1Assistant.Matrix.GetCellValue(mtx, key_column, matrix_row).ToString().Trim();
                if (currentKey.Length == 0)
                {
                    SBOApp.StatusBar.SetText(String.Format("“{0}”中列“{1}”不能为空", mtx_uid, key_column), SAPbouiCOM.BoMessageTime.bmt_Medium, SAPbouiCOM.BoStatusBarMessageType.smt_Warning);
                    return;
                }
                SAPBoAddon.B1Assistant.Matrix.RemoveNoValueLines(this.CurrentForm, mtx_uid, "col_f", key_column, dbds.TableName, "LineId");
                int dbds_row = -1;
                string current_LineId = SAPBoAddon.B1Assistant.Matrix.GetCellValue(mtx, "col_f", matrix_row).ToString().Trim();

                for (int i = 0; i < dbds.Size; i++)
                {
                    if (dbds.GetValue("LineId", i).Trim() == current_LineId)
                    {
                        dbds_row = i;
                        break;
                    }
                }
                if (dbds_row > -1)
                {
                    SAPBoAddon.B1Assistant.DBDataSource.AddNewRecordTo(dbds, 1, "LineId", "VisOrder");

                    string mtx_dbds_mapping = "col_FID:U_LineID";//mtx=源，dbds=目标
                    foreach (string column_alais in mtx_dbds_mapping.Split(';'))
                    {
                        if (columns.ContainsKey(column_alais.Split(':')[0]))
                        {
                            string FatherID = SAPBoAddon.B1Assistant.Matrix.GetCellValue(mtx, column_alais.Split(':')[0], mtx.VisualRowCount).ToString().Trim();
                            dbds.SetValue(column_alais.Split(':')[1], dbds.Offset, FatherID);
                            //copy dbds fields
                        }
                    }

                    IEnumerator enumerator_dbds = dbds.Fields.GetEnumerator();
                    while (enumerator_dbds.MoveNext())
                    {
                        SAPbouiCOM.Field field = enumerator_dbds.Current as SAPbouiCOM.Field;
                        if (field.Name.StartsWith("U_"))//只复制自定义字段
                        {
                            dbds.SetValue(field.Name, dbds.Offset, dbds.GetValue(field.Name, dbds_row));
                        }
                    }
                    mtx.AddRow();
                }
            }
            else
            {
                SBOApp.StatusBar.SetText(String.Format("“{0}”还没有选中行", mtx_uid), SAPbouiCOM.BoMessageTime.bmt_Medium, SAPbouiCOM.BoStatusBarMessageType.smt_Warning);
                //如果当前Matrix没有行，无从复制
            }
        }
        /// <summary>
        /// 复制Matrix行（集成方法）
        /// </summary>
        /// <param name="mtx_uid"></param>
        /// <param name="matrix_row"></param>
        private void CopyMatrixRow(string mtx_uid, int matrix_row)
        {
            switch (mtx_uid)
            {
                case "mtx_4":
                    CopyMatrix4Row(matrix_row, new List<string> { "U_TransportMode" });
                    SelectedRow("", (this.CurrentForm.Items.Item(mtx_uid).Specific as Matrix).VisualRowCount, "mtx_4", "mtx_1", AVA_QOT1, "col_14");
                    SelectedRow("", (this.CurrentForm.Items.Item(mtx_uid).Specific as Matrix).VisualRowCount, "mtx_4", "mtx_5", AVA_QOT5, "col_10");
                    (this.CurrentForm.Items.Item("mtx_2").Specific as Matrix).Clear();
                    (this.CurrentForm.Items.Item("mtx_5").Specific as Matrix).Clear();
                    //CopyMatrixRow(mtx_uid, AVA_FF_QOT4, "col_11", matrix_row);
                    break;
                case "mtx_1":
                    CopyMatrix1Row(matrix_row, new List<string> { "U_ItemName", "U_CargoType" });
                    SelectedRow("", (this.CurrentForm.Items.Item(mtx_uid).Specific as Matrix).VisualRowCount, "mtx_1", "mtx_2", AVA_QOT2, "col_15");
                    //CopyMatrixRow(mtx_uid, AVA_FF_QOT1, "col_14", matrix_row);
                    break;
                case "mtx_2":
                    CopyMatrixRow(mtx_uid, matrix_row, new List<string> { "U_Condition", "U_Value" });
                    //CopyMatrixRow(mtx_uid, AVA_FF_QOT2, "col_15", matrix_row);
                    break;
                case "mtx_5":
                    CopyMatrix5Row(matrix_row, new List<string> { "U_ItemName", "U_CargoType" });
                    SelectedRow("", (this.CurrentForm.Items.Item(mtx_uid).Specific as Matrix).VisualRowCount, "mtx_5", "mtx_6", AVA_QOT6, "col_15");
                    //CopyMatrixRow(mtx_uid, AVA_FF_QOT5, "col_10", matrix_row);
                    break;
                case "mtx_6":
                    CopyMatrixRow(mtx_uid, matrix_row, new List<string> { "U_Condition", "U_Value" });
                    //CopyMatrixRow(mtx_uid, AVA_FF_QOT6, "col_15", matrix_row);
                    break;
            }
        }
        #endregion
        #region 复制费用方案
        /// <summary>
        /// 复制费用方案
        /// </summary>
        /// <param name="ParentLineId"></param>
        /// <param name="FeeDocEntry"></param>
        private void CopyFeeScenario(string ParentLineId, string FeeDocEntry, string mtx1, string mtx2)
        {
            SAPbouiCOM.Matrix oMtx1;
            SAPbouiCOM.Matrix oMtx2;

            SAPbouiCOM.DBDataSource dbds_Qot1;
            SAPbouiCOM.DBDataSource dbds_Qot2;

            SAPbobsCOM.Recordset rs1;
            SAPbobsCOM.Recordset rs2;

            oMtx1 = CurrentForm.Items.Item(mtx1).Specific;
            oMtx2 = CurrentForm.Items.Item(mtx2).Specific;

            dbds_Qot1 = CurrentForm.DataSources.DBDataSources.Item(oMtx1.Columns.Item("col_f").DataBind.TableName);
            dbds_Qot2 = CurrentForm.DataSources.DBDataSources.Item(oMtx2.Columns.Item("col_f").DataBind.TableName);

            rs1 = SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
            rs2 = SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);

            string Lineid = string.Empty;
            rs1.DoQuery(String.Format("select DocEntry,LineId,U_CargoType,U_ItemCode,U_ItemName,U_Remarks from [@AVA_FF_FEE1] where DocEntry = '{0}'", FeeDocEntry));
            for (int i = 0; i < rs1.RecordCount; i++)
            {
                #region 判断是否存在空行
                if (string.IsNullOrEmpty(dbds_Qot1.GetValue("U_ItemName", dbds_Qot1.Size - 1).Trim()))
                {
                    if (dbds_Qot1.GetValue("U_LineId", dbds_Qot1.Size - 1).Trim() != ParentLineId)
                    {
                        dbds_Qot1.RemoveRecord(dbds_Qot1.Size - 1);
                        Lineid = SAPBoAddon.B1Assistant.DBDataSource.AddNewRecordTo(dbds_Qot1, 1, "LineId", "VisOrder").ToString();
                    }
                    else
                    {
                        Lineid = dbds_Qot1.Size.ToString();

                    }
                    int MatrixRow = SAPBoAddon.B1Assistant.Matrix.GetMatrixRow(oMtx1, dbds_Qot1, "col_f");
                    if (MatrixRow <= 0)
                    {
                        oMtx1.AddRow();
                    }
                }
                else
                {
                    Lineid = SAPBoAddon.B1Assistant.DBDataSource.AddNewRecordTo(dbds_Qot1, 1, "LineId", "VisOrder").ToString();
                    oMtx1.AddRow();
                }
                #endregion
                oMtx1.GetLineData(oMtx1.VisualRowCount);
                dbds_Qot1.SetValue("LineId", dbds_Qot1.Offset, Lineid);
                dbds_Qot1.SetValue("U_LineID", dbds_Qot1.Offset, ParentLineId);
                dbds_Qot1.SetValue("U_CargoType", dbds_Qot1.Offset, rs1.Fields.Item("U_CargoType").Value);
                dbds_Qot1.SetValue("U_ItemCode", dbds_Qot1.Offset, rs1.Fields.Item("U_ItemCode").Value.ToString());
                dbds_Qot1.SetValue("U_ItemName", dbds_Qot1.Offset, rs1.Fields.Item("U_ItemName").Value.ToString());
                //dbds_Qot1.SetValue("U_Remarks", dbds_Qot1.Offset, rs1.Fields.Item("U_Remarks").Value.ToString());
                oMtx1.SetLineData(oMtx1.VisualRowCount);

                #region 加载费用项执行条件
                rs2.DoQuery(String.Format(@"select U_Condition,U_Value,U_Currency,U_Rate,U_Price,U_VirPrice,U_Unit,U_Remarks 
                                            from [@AVA_FF_FEE2]
                                            where U_LineID = '{0}' and DocEntry = '{1}' "
                            , rs1.Fields.Item("LineId").Value.ToString().Trim()
                            , rs1.Fields.Item("DocEntry").Value.ToString().Trim()));
                for (int ii = 0; ii < rs2.RecordCount; ii++)
                {
                    #region 判断是否存在空行
                    string ChildLienid = string.Empty;
                    if (string.IsNullOrEmpty(dbds_Qot2.GetValue("U_Condition", dbds_Qot2.Size - 1).Trim()))
                    {
                        if (dbds_Qot2.GetValue("U_LineId", dbds_Qot2.Size - 1).Trim() != Lineid)
                        {
                            dbds_Qot2.RemoveRecord(dbds_Qot2.Size - 1);
                            ChildLienid = SAPBoAddon.B1Assistant.DBDataSource.AddNewRecordTo(dbds_Qot2, 1, "LineId", "VisOrder").ToString();
                        }
                        else
                        {
                            ChildLienid = dbds_Qot2.Size.ToString();
                        }
                        int MatrixRow = SAPBoAddon.B1Assistant.Matrix.GetMatrixRow(oMtx2, dbds_Qot2, "col_f");
                        if (MatrixRow <= 0)
                        {
                            oMtx2.AddRow();
                        }

                    }
                    else
                    {
                        ChildLienid = SAPBoAddon.B1Assistant.DBDataSource.AddNewRecordTo(dbds_Qot2, 1, "LineId", "VisOrder").ToString();
                        oMtx2.AddRow();
                    }
                    #endregion
                    oMtx2.GetLineData(oMtx2.VisualRowCount);
                    dbds_Qot2.SetValue("U_LineID", dbds_Qot2.Offset, Lineid);
                    dbds_Qot2.SetValue("U_Condition", dbds_Qot2.Offset, rs2.Fields.Item("U_Condition").Value.ToString());
                    dbds_Qot2.SetValue("U_Value", dbds_Qot2.Offset, rs2.Fields.Item("U_Value").Value.ToString());
                    dbds_Qot2.SetValue("U_Currency", dbds_Qot2.Offset, rs2.Fields.Item("U_Currency").Value.ToString());
                    dbds_Qot2.SetValue("U_Rate", dbds_Qot2.Offset, rs2.Fields.Item("U_Rate").Value.ToString());
                    dbds_Qot2.SetValue("U_Price", dbds_Qot2.Offset, rs2.Fields.Item("U_Price").Value.ToString());
                    dbds_Qot2.SetValue("U_VirPrice", dbds_Qot2.Offset, rs2.Fields.Item("U_VirPrice").Value.ToString());
                    dbds_Qot2.SetValue("U_Unit", dbds_Qot2.Offset, rs2.Fields.Item("U_Unit").Value.ToString());
                    dbds_Qot2.SetValue("U_Remarks", dbds_Qot2.Offset, rs2.Fields.Item("U_Remarks").Value.ToString());
                    oMtx2.SetLineData(oMtx2.VisualRowCount);

                    rs2.MoveNext();
                }
                #endregion

                rs1.MoveNext();
            }
        }
        #endregion
        #region 选择行
        /// <summary>
        /// 选择行
        /// </summary>
        /// <param name="ColUID"></param>
        /// <param name="Row"></param>
        /// <param name="ParentMatrixUID"></param>
        /// <param name="ChildMatrixUID"></param>
        /// <param name="ChildDataTable"></param>
        /// <param name="ChildCheckPrimaryCol"></param>
        private void SelectedRow(string ColUID, int Row, string ParentMatrixUID, string ChildMatrixUID, string ChildDataTable, string ChildCheckPrimaryCol)
        {
            if (CurrentForm.Mode == BoFormMode.fm_FIND_MODE) return;
            SAPbouiCOM.Matrix oMtxParent = CurrentForm.Items.Item(ParentMatrixUID).Specific;
            SAPbouiCOM.Matrix oMtxChild = CurrentForm.Items.Item(ChildMatrixUID).Specific;
            SAPbouiCOM.DBDataSource dbds = CurrentForm.DataSources.DBDataSources.Item(ChildDataTable);
            //CurrentForm.Freeze(true);
            #region 港口的Matrix中被选中的行
            int SelectedRow = -1;
            if (Row > 0 && ColUID != "col_f")
            {
                //SAPbouiCOM.CellPosition oPosition = oMtxParent.get
                //if (oPosition == null) return;
                //SelectedRow = oPosition.rowIndex;
                oMtxParent.SelectRow(Row, true, false);
            }
            else
            {
                if (Row > 0 && oMtxParent.GetCellFocus() != null)
                {
                    SelectedRow = oMtxParent.GetNextSelectedRow();
                    if (CurrentForm.ActiveItem == ParentMatrixUID)
                    {
                        SAPbouiCOM.CellPosition oPosition = oMtxParent.GetCellFocus();
                        if (oPosition == null) return;
                        oMtxParent.SetCellFocus(SelectedRow, oPosition.ColumnIndex);
                    }
                }
                else
                {
                    SelectedRow = Row;
                }
            }
            #endregion
            if (SelectedRow > 0)
            {
                string LineId = string.Empty;
                dynamic Item = oMtxParent.GetCellSpecific("col_f", SelectedRow);
                LineId = Item.Value;
                #region 显示费用项中对应的港口
                if (CurrentForm.Mode != BoFormMode.fm_OK_MODE && CurrentForm.Mode != BoFormMode.fm_VIEW_MODE)
                {
                    SAPBoAddon.B1Assistant.Matrix.UpdateDataTodbDataSource(CurrentForm, oMtxChild, dbds, "col_f", "LineId", ChildCheckPrimaryCol);
                }
                oMtxChild.Clear();
                for (int i = 0; i < dbds.Size; i++)
                {
                    if (dbds.GetValue("U_Lineid", i).Trim() == LineId)
                    {
                        dbds.Offset = i;
                        oMtxChild.AddRow();
                    }
                }
                if (CurrentForm.Mode == BoFormMode.fm_ADD_MODE || CurrentForm.Mode == BoFormMode.fm_UPDATE_MODE)
                {
                    AddRow(ParentMatrixUID, ChildMatrixUID, ChildDataTable, ChildCheckPrimaryCol);
                }
                #endregion
            }
            else
            {
                #region 显示所有的费用项
                if (CurrentForm.Mode != BoFormMode.fm_OK_MODE && CurrentForm.Mode != BoFormMode.fm_VIEW_MODE)
                    SAPBoAddon.B1Assistant.Matrix.UpdateDataTodbDataSource(CurrentForm, oMtxChild, dbds, "col_f", "LineId", ChildCheckPrimaryCol);
                oMtxChild.Clear();
                for (int i = 0; i < dbds.Size; i++)
                {
                    dbds.Offset = i;
                    oMtxChild.AddRow();
                }
                #endregion
            }
            //CurrentForm.Freeze(false);

        }
        #endregion
        #region 添加行
        /// <summary>
        /// 添加行
        /// </summary>
        /// <param name="ParentMatrixUID"></param>
        /// <param name="ChildMatrixUID"></param>
        /// <param name="ChildDataTable"></param>
        /// <param name="ChildCheckPrimaryCol"></param>
        private void AddRow(string ParentMatrixUID, string ChildMatrixUID, string ChildDataTable, string ChildCheckPrimaryCol, string MatrixValue = null)
        {
            if (CurrentForm.Mode == BoFormMode.fm_FIND_MODE) return;
            SAPbouiCOM.Matrix oMtxParent = CurrentForm.Items.Item(ParentMatrixUID).Specific;//港口
            SAPbouiCOM.Matrix oMtxChild = CurrentForm.Items.Item(ChildMatrixUID).Specific;//费用项
            int SelectedRow = oMtxParent.GetNextSelectedRow();
            if (SelectedRow <= 0)
            {
                SBOApp.MessageBox("请选择要为哪一个" + MatrixValue + "添加执行条件。");
                return;
            }
            string Lineid = string.Empty;
            dynamic item = oMtxParent.GetCellSpecific("col_f", SelectedRow);
            Lineid = item.Value;
            int CurrentRow = SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(this.CurrentForm, ChildMatrixUID, "col_f", ChildDataTable, -1, "Lineid", "VisOrder", true, ChildCheckPrimaryCol);
            if (CurrentRow == -1) return;
            //设置Matrix行的默认值
            //SetMatrixRowDefaultValue(ChildMatrixUID, oMtxChild.VisualRowCount, Matrix1_DefaultValue);
            item = oMtxChild.GetCellSpecific("col_FID", CurrentRow);
            item.Value = Lineid;
            if (ParentMatrixUID == "mtx_4")
            {
                InitialNewRow(ChildMatrixUID, new string[] { "cargo_cty" });
            }
        }
        private void InitialNewRow(string mtxUID, string[] mapping)
        {
            if (CurrentForm.Mode == BoFormMode.fm_FIND_MODE) return;
            if (mapping == null || mapping.Length == 0) return;

            SAPbouiCOM.Matrix mtx = CurrentForm.Items.Item(mtxUID).Specific;
            if (mtx.VisualRowCount > 1)
            {
                foreach (var item in mapping)
                {
                    dynamic cell = mtx.GetCellSpecific(item, 1);
                    dynamic combo = mtx.GetCellSpecific(item, mtx.VisualRowCount);
                    (combo as ComboBox).Select(cell.Value, BoSearchKey.psk_ByValue);
                }
            }
        }
        #endregion
        #region 删除行
        /// <summary>
        /// 港口删除行
        /// </summary>
        private void PortDeleteRow(string ParentMatrixUID, string ParentDataTable, string ChildMatrixUID, string ChildDataTable, string GrandChildMatrixUID, string GrandChildDataTable)
        {
            SAPbouiCOM.Matrix oMtxParent = CurrentForm.Items.Item(ParentMatrixUID).Specific;
            SAPbouiCOM.Matrix oMtxChild = CurrentForm.Items.Item(ChildMatrixUID).Specific;
            SAPbouiCOM.Matrix oMtxGrandchild = CurrentForm.Items.Item(GrandChildMatrixUID).Specific;
            SAPbouiCOM.DBDataSource dbdsChild = CurrentForm.DataSources.DBDataSources.Item(ChildDataTable);
            SAPbouiCOM.DBDataSource dbdsGrandchild = CurrentForm.DataSources.DBDataSources.Item(GrandChildDataTable);

            //根源Matrix中的选中行
            int SelectedRow = oMtxParent.GetNextSelectedRow();
            //根据Matrix中的关键行号
            string ParentLineId = string.Empty;
            dynamic Item = oMtxParent.GetCellSpecific("col_f", SelectedRow);
            ParentLineId = Item.Value;
            //在子Matrix中查找属于根源的关键行号
            for (int i = 0; i < dbdsChild.Size; i++)
            {
                //判断是否等于根源的关键行号
                if (dbdsChild.GetValue("U_Lineid", i).Trim() == ParentLineId)
                {
                    //获取子数据中的关键行号
                    string ChildLineId = dbdsChild.GetValue("Lineid", i).Trim();
                    //在孙子数据中查找属于子数据的关键行号
                    for (int j = 0; j < dbdsGrandchild.Size; j++)
                    {
                        //判断是否等于子数据中的关键行号
                        if (dbdsGrandchild.GetValue("U_Lineid", j).Trim() == ChildLineId)
                        {
                            dbdsGrandchild.Offset = j;
                            int GrandchildRow = SAPBoAddon.B1Assistant.Matrix.GetMatrixRow(oMtxGrandchild, dbdsGrandchild, "col_f");
                            dbdsGrandchild.RemoveRecord(j);
                            if (GrandchildRow > 0 && GrandchildRow <= oMtxGrandchild.VisualRowCount)
                            {
                                oMtxGrandchild.DeleteRow(GrandchildRow);
                            }
                            j--;
                        }
                    }
                    dbdsChild.Offset = i;
                    int ChildRow = SAPBoAddon.B1Assistant.Matrix.GetMatrixRow(oMtxChild, dbdsChild, "col_f");
                    dbdsChild.RemoveRecord(i);
                    if (ChildRow > 0 && ChildRow <= oMtxChild.VisualRowCount)
                    {
                        oMtxChild.DeleteRow(ChildRow);
                    }
                    i--;
                }
            }
            //自身所处的Matrix
            SAPBoAddon.B1Assistant.Matrix.DeleteRowAndDbdsLine(CurrentForm, ParentDataTable, "Lineid", ParentMatrixUID, "col_f", SelectedRow, true);
        }
        /// <summary>
        /// 费用项删除行
        /// </summary>
        private void ChargeDeleteRow(string ParentMatrixUID, string ParentDataTable, string ChildMatrixUID, string ChildDataTable)
        {
            SAPbouiCOM.Matrix oMtxParent = CurrentForm.Items.Item(ParentMatrixUID).Specific;
            SAPbouiCOM.Matrix oMtxChild = CurrentForm.Items.Item(ChildMatrixUID).Specific;
            SAPbouiCOM.DBDataSource dbds = CurrentForm.DataSources.DBDataSources.Item(ChildDataTable);

            int SelectedRow = oMtxParent.GetNextSelectedRow();

            string LineId = string.Empty;
            dynamic Item = oMtxParent.GetCellSpecific("col_f", SelectedRow);
            LineId = Item.Value;

            for (int i = 0; i < dbds.Size; i++)
            {
                if (dbds.GetValue("U_Lineid", i).Trim() == LineId)
                {
                    dbds.Offset = i;
                    int row = SAPBoAddon.B1Assistant.Matrix.GetMatrixRow(oMtxChild, dbds, "col_f");
                    dbds.RemoveRecord(i);
                    if (row > 0 && row <= oMtxChild.VisualRowCount)
                    {
                        oMtxChild.DeleteRow(row);
                    }
                    i--;
                }
            }
            //自身所处的Matrix
            SAPBoAddon.B1Assistant.Matrix.DeleteRowAndDbdsLine(CurrentForm, ParentDataTable, "Lineid", ParentMatrixUID, "col_f", SelectedRow, true);
        }
        #endregion
        #region ComboBox设置
        /// <summary>
        /// 将业务员添加到ComboBox中
        /// </summary>
        /// <param name="itemuid"></param>
        /// <param name="select_current"></param>
        private void InitialMatrixUserComboBox(string itemuid, bool select_current)
        {
            Recordset rs = this.SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
            string select = String.Format("select empID, t0.userId,jobTitle as empName from OHEM t0 left join OUSR t1 on t0.userId = t1.USERID");// where t0.userId =" + this.SBOCompany.UserSignature.ToString());
            if (select_current)
            {
                select += " where t0.userId =" + this.SBOCompany.UserSignature.ToString();
            }
            rs.DoQuery(select);
            ComboBox cb = this.CurrentForm.Items.Item(itemuid).Specific;
            for (int i = 0; i < cb.ValidValues.Count; i++)
            {
                cb.ValidValues.Remove(i, BoSearchKey.psk_Index);
            }
            for (int i = 0; i < rs.RecordCount; i++)
            {
                cb.ValidValues.Add(rs.Fields.Item("empID").Value.ToString(), rs.Fields.Item("empName").Value.ToString());
                rs.MoveNext();
            }
            if (select_current)
            {
                if (cb.ValidValues.Count > 0)
                {
                    cb.Select(0, BoSearchKey.psk_Index);
                }
            }
        }
        /// <summary>
        /// 初始化下拉列表
        /// </summary>
        /// <author>WangPeng</author>
        private void Combobox_Initialize()
        {
            SAPbobsCOM.Recordset oRst = SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
            Utils.ComboBox_Cargo_Initial(oRst, this.CurrentForm, Cargo_ComboBoxUID_Field);

            Utils.ComboBox_InitialItem(oRst, this.CurrentForm.Items.Item("321"), "Code", "Name", "@AVA_FF_BAMD");
            Utils.InitialMatrixComboBox(SBOCompany, this.CurrentForm.Items.Item("mtx_1").Specific, "cargo_cty", "Code", "Name", "@AVA_FF_CATY");/*货物类型*/
            Utils.InitialMatrixComboBox(SBOCompany, this.CurrentForm.Items.Item("mtx_5").Specific, "cargo_cty", "Code", "Name", "@AVA_FF_CATY");/*货物类型*/

            Utils.InitialMatrixComboBox(SBOCompany, this.CurrentForm.Items.Item("mtx_6").Specific, "col_17", "DocCurrCod", "CurrName", "OCRN");
            Utils.InitialMatrixComboBox(SBOCompany, this.CurrentForm.Items.Item("mtx_2").Specific, "col_17", "DocCurrCod", "CurrName", "OCRN");
            string type = string.Empty;
            if (CurrentForm.TypeEx.Contains("_FF_"))
            {
                type = "Air";
                Utils.InitialMatrixComboBox(SBOCompany, this.CurrentForm.Items.Item("mtx_4").Specific, "col_23", "Code", "Name", "@AVA_FF_OASC");//空运服务编码是空运特有的
            }
            if (CurrentForm.TypeEx.Contains("_SE_"))
            {
                type = "Sea";
            }
            if (CurrentForm.TypeEx.Contains("_LN_"))
            {
                type = "Land";
            }
            Utils.InitialMatrixComboBox(SBOCompany, this.CurrentForm.Items.Item("mtx_4").Specific, "col_9", "Code", "Name", "@AVA_FF_OMOT", null, null, String.IsNullOrEmpty(type) ? null : String.Format("U_Type='{0}' or U_Type IS NULL", type));//运输方式
            //Utils.InitialMatrixComboBox(SBOCompany, this.CurrentForm.Items.Item("mtx_3").Specific, "col_15", "Code", "Name", "@AVA_FF_CATY");//货物类型
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("326"), "Code", "Name", "@AVA_FF_DTYP", "CAST(Code AS INT)");//文件类型 added by WangPeng 2012年12月13日 14:44:12
        }
        /// <summary>
        /// ComboBox选默认值
        /// </summary>
        /// <author>WangPeng</author>
        private void ComboBox_SelectDefault()
        {
            string DefaultVal = string.Empty;
            SAPbouiCOM.DBDataSource dbds_oqot = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT);
            this.CurrentForm.Freeze(true);
            try
            {
                Utils.ComboBox_Cargo_SelectDefault(this.CurrentForm, Cargo_ComboBoxUID_Field, AVA_QOT3);
                DefaultVal = Utils.ComboBox_SelectDefault("Code", "Name", "@AVA_FF_BAMD");
                dbds_oqot.SetValue("U_Bargain", dbds_oqot.Offset, DefaultVal);/*成交方式*/

                this.CurrentForm.Refresh();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.CurrentForm.Freeze(false);
            }
        }
        #endregion
        #region 提/送货地址
        /// <summary>
        /// 获取提/送货地址
        /// </summary>
        private void GetAddress()
        {
            SAPbouiCOM.Matrix oMtx = CurrentForm.Items.Item("mtx_1").Specific;
            SAPbouiCOM.DBDataSource dbds = CurrentForm.DataSources.DBDataSources.Item(AVA_QOT1);
            int SelectedRow = oMtx.GetNextSelectedRow();
            if (SelectedRow <= 0) return;
            string Address = string.Empty;
            dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(oMtx, SelectedRow, "col_f", dbds);
            Address = dbds.GetValue("U_Address", dbds.Offset);
            CurrentForm.DataSources.UserDataSources.Item("PackUPAddr").ValueEx = Address;
        }
        /// <summary>
        /// 设置Matrix的提/送货地址
        /// </summary>
        private void SetMatrixAddress()
        {
            SAPbouiCOM.Matrix oMtx = CurrentForm.Items.Item("mtx_1").Specific;
            SAPbouiCOM.DBDataSource dbds = CurrentForm.DataSources.DBDataSources.Item(AVA_QOT1);
            int SelectedRow = oMtx.GetNextSelectedRow();
            dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(oMtx, SelectedRow, "col_f", dbds);
            oMtx.GetLineData(SelectedRow);

            oMtx.SetLineData(SelectedRow);
        }
        /// <summary>
        /// 从Item设置中设置提/送货地址
        /// </summary>
        private void SetAddress()
        {
            SAPbouiCOM.Matrix oMtx = CurrentForm.Items.Item("mtx_1").Specific;
            SAPbouiCOM.DBDataSource dbds = CurrentForm.DataSources.DBDataSources.Item(AVA_QOT1);
            int SelectedRow = oMtx.GetNextSelectedRow();
            if (SelectedRow > 0)
            {
                string Address = string.Empty;
                dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(oMtx, SelectedRow, "col_f", dbds);
                Address = CurrentForm.DataSources.UserDataSources.Item("PackUPAddr").ValueEx;
                oMtx.GetLineData(SelectedRow);
                dbds.SetValue("U_Address", dbds.Offset, Address);
                oMtx.SetLineData(SelectedRow);
            }
        }
        #endregion
        #region 费用及港口的添加行
        private void Add()
        {
            SAPbouiCOM.Form oForm;
            SAPbouiCOM.Item oItem;
            SAPbouiCOM.Matrix oMtx;
            string TableName = string.Empty;
            try
            {
                oForm = SBOApp.Forms.ActiveForm;
                if (oForm.Mode != BoFormMode.fm_ADD_MODE && oForm.Mode != BoFormMode.fm_UPDATE_MODE && oForm.Mode != BoFormMode.fm_OK_MODE)
                    return;
                if (oForm.ActiveItem == "")
                {
                    SBOApp.MessageBox("请选择要添加行的表格。");
                    return;
                }
                switch (CurrentMatrix)
                {
                    case "mtx_4"://港口添加行
                        oMtx = CurrentForm.Items.Item("mtx_4").Specific;
                        SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(CurrentForm, "mtx_4", "col_f", AVA_QOT4, -1, "Lineid", "VisOrder", true, "col_10");
                        //SetMatrixRowDefaultValue("mtx_4", oMtx.VisualRowCount, Matrix4_DefaultValue);
                        break;
                    case "mtx_1"://费用项添加行
                        AddRow("mtx_4", "mtx_1", AVA_QOT1, "col_14", "港口");
                        break;
                    case "mtx_2"://费用项的执行条件添加行
                        AddRow("mtx_1", "mtx_2", AVA_QOT2, "col_15", "费用项");
                        break;
                    case "mtx_5"://底价费用项添加行
                        AddRow("mtx_4", "mtx_5", AVA_QOT5, "col_10", "港口");
                        break;
                    case "mtx_6"://底价费用项的执行条件添加行
                        AddRow("mtx_5", "mtx_6", AVA_QOT6, "col_15", "底价费用项");
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #endregion
    }
}
